diff options
author | John Vandenberg <jayvdb@gmail.com> | 2016-05-06 03:16:10 +0700 |
---|---|---|
committer | Ian Cordasco <sigmavirus24@users.noreply.github.com> | 2016-05-05 15:16:10 -0500 |
commit | 45c28a31d3f4c402056433da2a7eb29f565e4344 (patch) | |
tree | 82b45b64a377c2c71ad3fc5a0f86c1f427a95fde | |
parent | 4361b6d0733837e4d0e402c41cb55961bd43a5d7 (diff) | |
download | pyflakes-45c28a31d3f4c402056433da2a7eb29f565e4344.tar.gz |
Suppress RedefinedWhileUnused for submodule import (#62)
Fixes lp:1578051
aec68a7 added module names to error messages, which included
a new class SubmoduleImportation to handle the special case
of submodule imports. It correctly handled the case of
a submodule import occurring after the root module was imported,
but didnt handle the opposite case of the submodule import
occurring before the root module was imported.
-rw-r--r-- | pyflakes/checker.py | 22 | ||||
-rw-r--r-- | pyflakes/test/test_imports.py | 6 |
2 files changed, 24 insertions, 4 deletions
diff --git a/pyflakes/checker.py b/pyflakes/checker.py index 5ff1480..f499727 100644 --- a/pyflakes/checker.py +++ b/pyflakes/checker.py @@ -142,6 +142,9 @@ class Importation(Definition): super(Importation, self).__init__(name, source) def redefines(self, other): + if isinstance(other, SubmoduleImportation): + # See note in SubmoduleImportation about RedefinedWhileUnused + return self.fullName == other.fullName return isinstance(other, Definition) and self.name == other.name def _has_alias(self): @@ -165,13 +168,24 @@ class Importation(Definition): class SubmoduleImportation(Importation): + """ + A binding created by a submodule import statement. + + A submodule import is a special case where the root module is implicitly + imported, without an 'as' clause, and the submodule is also imported. + Python does not restrict which attributes of the root module may be used. + + This class is only used when the submodule import is without an 'as' clause. + + pyflakes handles this case by registering the root module name in the scope, + allowing any attribute of the root module to be accessed. + + RedefinedWhileUnused is suppressed in `redefines` unless the submodule + name is also the same, to avoid false positives. + """ def __init__(self, name, source): # A dot should only appear in the name when it is a submodule import - # without an 'as' clause, which is a special type of import where the - # root module is implicitly imported, and the submodules are also - # accessible because Python does not restrict which attributes of the - # root module may be used. assert '.' in name and (not source or isinstance(source, ast.Import)) package_name = name.split('.')[0] super(SubmoduleImportation, self).__init__(package_name, source) diff --git a/pyflakes/test/test_imports.py b/pyflakes/test/test_imports.py index 41a0e07..9cbd4d7 100644 --- a/pyflakes/test/test_imports.py +++ b/pyflakes/test/test_imports.py @@ -767,6 +767,12 @@ class Test(TestCase): fu.x ''') + self.flakes(''' + import fu.bar + import fu + fu.x + ''') + def test_unused_package_with_submodule_import(self): """ When a package and its submodule are imported, only report once. |