Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress RedefinedWhileUnused for submodule import #62

Merged
merged 1 commit into from May 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 18 additions & 4 deletions pyflakes/checker.py
Expand Up @@ -142,6 +142,9 @@ def __init__(self, name, source, full_name=None):
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):
Expand All @@ -165,13 +168,24 @@ def __str__(self):


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)
Expand Down
6 changes: 6 additions & 0 deletions pyflakes/test/test_imports.py
Expand Up @@ -767,6 +767,12 @@ def test_used_package_with_submodule_import(self):
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.
Expand Down