Skip to content

Commit

Permalink
Make the Binding parameter a constructor and use it for all bindings.
Browse files Browse the repository at this point in the history
  • Loading branch information
PetterS committed Apr 15, 2020
1 parent 44aab70 commit e2b27b9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
29 changes: 14 additions & 15 deletions pyflakes/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,11 @@ class Binding(object):
the node that this binding was last used.
"""

def __init__(self, name, source):
def __init__(self, name, source, during_type_checking=False):
self.name = name
self.source = source
self.used = False
self.during_type_checking = False
self.during_type_checking = during_type_checking

def __str__(self):
return self.name
Expand Down Expand Up @@ -1074,6 +1074,8 @@ def addBinding(self, node, value):
break
existing = scope.get(value.name)

value.during_type_checking = self._in_type_checking

if (existing and not isinstance(existing, Builtin) and
not self.differentForks(node, existing.source)):

Expand Down Expand Up @@ -1162,15 +1164,15 @@ def handleNodeLoad(self, node):
# alias of other Importation and the alias
# is used, SubImportation also should be marked as used.
n = scope[name]
if isinstance(n, Importation):
if n._has_alias():
try:
scope[n.fullName].used = (self.scope, node)
except KeyError:
pass
if n.during_type_checking and not self._in_annotation:
# Only defined during type-checking; this does not count.
continue
if isinstance(n, Importation) and n._has_alias():
try:
scope[n.fullName].used = (self.scope, node)
except KeyError:
pass
if n.during_type_checking and not self._in_annotation:
# Only defined during type-checking; this does not count. Real code
# (not an annotation) using this binding will not work.
continue
except KeyError:
pass
else:
Expand Down Expand Up @@ -2131,7 +2133,6 @@ def IMPORT(self, node):
else:
name = alias.asname or alias.name
importation = Importation(name, node, alias.name)
importation.during_type_checking = self._in_type_checking
self.addBinding(node, importation)

def IMPORTFROM(self, node):
Expand Down Expand Up @@ -2164,9 +2165,7 @@ def IMPORTFROM(self, node):
self.report(messages.ImportStarUsed, node, module)
importation = StarImportation(module, node)
else:
importation = ImportationFrom(name, node,
module, alias.name)
importation.during_type_checking = self._in_type_checking
importation = ImportationFrom(name, node, module, alias.name)
self.addBinding(node, importation)

def TRY(self, node):
Expand Down
10 changes: 10 additions & 0 deletions pyflakes/test/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,16 @@ def test_ignoresTypingImports(self):
b()
''', m.UndefinedName)

def test_ignoresTypingClassDefinitino(self):
"""Ignores imports within 'if TYPE_CHECKING' checking normal code."""
self.flakes('''
from typing import TYPE_CHECKING
if TYPE_CHECKING:
class T:
...
t = T()
''', m.UndefinedName)

def test_usesTypingImportsForAnnotations(self):
"""Uses imports within 'if TYPE_CHECKING' checking annotations."""
self.flakes('''
Expand Down

0 comments on commit e2b27b9

Please sign in to comment.