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

Globally imported dunders are implicitly used. #645

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pyflakes/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,10 @@ def getNodeName(node):
return node.name


def _is_dunder(name):
return name.startswith("__") and name.endswith("__") and len(name) > 4


TYPING_MODULES = frozenset(('typing', 'typing_extensions'))


Expand Down Expand Up @@ -1026,7 +1030,9 @@ def checkDeadScopes(self):
# Look for imported names that aren't used.
for value in scope.values():
if isinstance(value, Importation):
used = value.used or value.name in all_names
used = (value.used or value.name in all_names
or isinstance(scope, ModuleScope)
and _is_dunder(value.name))
if not used:
messg = messages.UnusedImport
self.report(messg, value.source, str(value))
Expand Down
6 changes: 6 additions & 0 deletions pyflakes/test/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ def test_usedImport_relative(self):
self.flakes('from .. import fu; assert fu')
self.flakes('from ..bar import fu as baz; assert baz')

def test_implicitlyUsedDunderImport(self):
self.flakes('from foo import __author__')
self.flakes('from foo import version as __version__')
self.flakes('from .foo import __author__')
self.flakes('from .foo import version as __version__')

def test_redefinedWhileUnused(self):
self.flakes('import fu; fu = 3', m.RedefinedWhileUnused)
self.flakes('import fu; fu, bar = 3', m.RedefinedWhileUnused)
Expand Down