TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

Bug #1560134 reported by Miguel Latorre
36
This bug affects 7 people
Affects Status Importance Assigned to Milestone
Pyflakes
Fix Released
Critical
John Vandenberg
pyflakes (Debian)
Fix Released
Unknown
pyflakes (Ubuntu)
Fix Released
Critical
Barry Warsaw

Bug Description

This simple code make pyflakes throw and exception (python 2.7.11 and 3.5.1).
-------------------------------------------
# -*- coding: utf-8 -*-

from . import foo

class MyClass(object):
    pass
-------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python3.5/runpy.py", line 170, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "pyflakes/__main__.py", line 5, in <module>
    main(prog='pyflakes')
  File "/home/mal/develop/python/pyflakes/pyflakes/api.py", line 184, in main
    warnings = checkRecursive(args, reporter)
  File "/home/mal/develop/python/pyflakes/pyflakes/api.py", line 141, in checkRecursive
    warnings += checkPath(sourcePath, reporter)
  File "/home/mal/develop/python/pyflakes/pyflakes/api.py", line 108, in checkPath
    return check(codestr, filename, reporter)
  File "/home/mal/develop/python/pyflakes/pyflakes/api.py", line 69, in check
    w = checker.Checker(tree, filename)
  File "/home/mal/develop/python/pyflakes/pyflakes/checker.py", line 403, in __init__
    self.handleChildren(tree)
  File "/home/mal/develop/python/pyflakes/pyflakes/checker.py", line 725, in handleChildren
    self.handleNode(node, tree)
  File "/home/mal/develop/python/pyflakes/pyflakes/checker.py", line 772, in handleNode
    handler(node)
  File "/home/mal/develop/python/pyflakes/pyflakes/checker.py", line 1151, in IMPORTFROM
    node.module, alias.name)
  File "/home/mal/develop/python/pyflakes/pyflakes/checker.py", line 199, in __init__
    full_name = module + '.' + self.real_name
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

Revision history for this message
Phil Frost (bitglue) wrote :

I'm pretty sure this is fixed in the current version of pyflakes. What version are you using?

Revision history for this message
Miguel Latorre (ma-latorre-diaz) wrote :

Version: 1.1.0
Latest commit: aec68a7847d8dbd1371242f42f9302103a68178f

commit aec68a7847d8dbd1371242f42f9302103a68178f
Author: John Vandenberg <email address hidden>
Date: Thu Nov 26 01:24:53 2015 +1100

    Importation classes with imported name and alias

    In order to solve many corner cases related to imports,
    more information is needed about each import.

    This change creates two new classes:
    - SubmoduleImportation
    - ImportationFrom

    And adds an optional parameter full_name to the super class
    Importation.

    Functionally, this change only improves existing error messages
    to report the full imported name where previously an error
    would include only the import alias.

Revision history for this message
Miguel Latorre (ma-latorre-diaz) wrote :

I'm a mercurial user so, just in case:
> git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

Revision history for this message
Phil Frost (bitglue) wrote :

Hm...1.1.0 is pretty recent. I'll have to investigate this more when I have some time. Thanks for the report.

Revision history for this message
predder (predder) wrote :

This happens for me in 1.2 but not 1.0 or 1.1.

nathan@w530 ~/code/inbox-persist ♪ flake8 inboxpersist/
Traceback (most recent call last):
  File "/usr/local/bin/flake8", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python2.7/dist-packages/flake8/main.py", line 33, in main
    report = flake8_style.check_files()
  File "/usr/local/lib/python2.7/dist-packages/flake8/engine.py", line 180, in check_files
    return self._retry_serial(self._styleguide.check_files, paths=paths)
  File "/usr/local/lib/python2.7/dist-packages/flake8/engine.py", line 171, in _retry_serial
    return func(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pep8.py", line 1840, in check_files
    self.input_dir(path)
  File "/usr/local/lib/python2.7/dist-packages/pep8.py", line 1876, in input_dir
    runner(os.path.join(root, filename))
  File "/usr/local/lib/python2.7/dist-packages/flake8/engine.py", line 125, in input_file
    return fchecker.check_all(expected=expected, line_offset=line_offset)
  File "/usr/local/lib/python2.7/dist-packages/pep8.py", line 1574, in check_all
    self.check_ast()
  File "/usr/local/lib/python2.7/dist-packages/pep8.py", line 1520, in check_ast
    checker = cls(tree, self.filename)
  File "/usr/local/lib/python2.7/dist-packages/flake8/_pyflakes.py", line 43, in __init__
    withDoctest=self.withDoctest)
  File "/usr/local/lib/python2.7/dist-packages/pyflakes/checker.py", line 402, in __init__
    self.handleChildren(tree)
  File "/usr/local/lib/python2.7/dist-packages/pyflakes/checker.py", line 724, in handleChildren
    self.handleNode(node, tree)
  File "/usr/local/lib/python2.7/dist-packages/pyflakes/checker.py", line 771, in handleNode
    handler(node)
  File "/usr/local/lib/python2.7/dist-packages/pyflakes/checker.py", line 1150, in IMPORTFROM
    node.module, alias.name)
  File "/usr/local/lib/python2.7/dist-packages/pyflakes/checker.py", line 198, in __init__
    full_name = module + '.' + self.real_name
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

Revision history for this message
John Vandenberg (jayvdb) wrote :

fwiw, there is a version mixup at the moment, with pypi providing 1.2.0 but master is still 1.1.0
(https://github.com/pyflakes/pyflakes/blob/master/pyflakes/__init__.py)

Thanks for the report Miguel. I can see where the problem is, and should have a fix shortly.

Revision history for this message
Phil Frost (bitglue) wrote :

I just updated master. Forgot to push last night when I made the release :)

Changed in pyflakes:
importance: Undecided → Critical
Revision history for this message
John Vandenberg (jayvdb) wrote :

fwiw, I have a fix https://github.com/jayvdb/pyflakes/commit/5c039f90b78fb4eb2b10287295f411e86baa8233

But I am working on another approach, as relative imports can also be star imports, and it would be nice to fix the error messages for star import at the same time. Should only be an hour or so.

Revision history for this message
John Vandenberg (jayvdb) wrote :
Revision history for this message
Patrick Dunnigan (patrick-m-dunnigan) wrote :

This is still a problem in 1.2.2. Cannot run pyflakes whatsoever.

Revision history for this message
Phil Frost (bitglue) wrote :

If you'd like to see a fix faster, feel free to review https://github.com/pyflakes/pyflakes/pull/61

John Vandenberg (jayvdb)
summary: - checkr.py; TypeError: unsupported operand type(s) for +: 'NoneType' and
- 'str'
+ TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
Changed in pyflakes:
assignee: nobody → John Vandenberg (jayvdb)
Scott Moser (smoser)
Changed in pyflakes (Ubuntu):
status: New → Confirmed
importance: Undecided → Critical
Revision history for this message
Scott Moser (smoser) wrote :

Just ran into this in Ubuntu Yakkety which and filed bug 1580214. Then duped that here.
Recreate is as simple as:

$ mkdir x
$ echo "FOO = 1" > x/__init__.py
$ echo "from . import FOO" > x/wark.py
$ python -m pyflakes x/

Revision history for this message
Barry Warsaw (barry) wrote :

This currently only affects Yakkety (or a PyPI or from-source install). Xenial isn't affected. I've subscribed to the upstream bug and will cherry pick it once they land the fix, or upload a new upstream if that's provided soon after.

Revision history for this message
Miguel Latorre (ma-latorre-diaz) wrote :

My current workaround is changing checker.py:212 from:

full_name = module + '.' + self.real_name

to

full_name = (module or '') + '.' + self.real_name

Revision history for this message
Scott Moser (smoser) wrote :

well, heres to hoping there is something for barry to cherry pick soon.
Currently anything that uses this in its build is probably FTBFS (fails to build from source). Example is curtin. I'm forced to skip or wait to upload.

I'm sure that curtin isn't the only thing that does this.

Barry Warsaw (barry)
Changed in pyflakes (Ubuntu):
status: Confirmed → In Progress
assignee: nobody → Barry Warsaw (barry)
Revision history for this message
Barry Warsaw (barry) wrote :

@ma-latorre-diaz: That fixes it for me too. I've added a DEP-8 test for this case, and a quilt patch as you suggest. I see no other regressions, so I'll upload this fix to Debian and it should autosync to Yakkety soon enough. We can remove the patch when a new upstream with the fix is available. Thanks!

Changed in pyflakes (Debian):
status: Unknown → Fix Released
Revision history for this message
John Vandenberg (jayvdb) wrote :

With that one-line fix, the error message reported will be wrong. The correct fix is in the github PR.

Revision history for this message
Barry Warsaw (barry) wrote : Re: [Bug 1560134] Re: TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

On May 12, 2016, at 12:50 AM, John Vandenberg wrote:

>With that one-line fix, the error message reported will be wrong. The
>correct fix is in the github PR.

I'm subscribed to the PR and will update the patch once that gets accepted and
merged. It looks like there's still some discussion going on about it?

For now, it's good just that pyflakes doesn't crash.

Revision history for this message
John Vandenberg (jayvdb) wrote :

The current patch addresses all previous review comments by reviewers.

Revision history for this message
Barry Warsaw (barry) wrote :

On May 12, 2016, at 03:29 PM, John Vandenberg wrote:

>The current patch addresses all previous review comments by reviewers.

I'll try it out instead of the current quick hack. Once a new upstream is
released with the fix, I'll upload it dropping the patch.

Barry Warsaw (barry)
Changed in pyflakes (Ubuntu):
status: In Progress → Fix Released
Changed in pyflakes:
status: New → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.