Skip to content
Closed
Changes from 1 commit
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
18 changes: 14 additions & 4 deletions pyflakes/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ def __init__(self, name, source):
self.name = name
self.source = source
self.used = False
self.during_type_checking = False

def __str__(self):
return self.name
Expand Down Expand Up @@ -588,6 +587,17 @@ def _add_to_names(container):

class Scope(dict):
importStarred = False # set to True when import * is found
# Special key for checking whether a binding is defined only for type checking.
TYPE_CHECKING_ONLY = object()

def __init__(self):
super(Scope, self).__init__(self)
self[self.TYPE_CHECKING_ONLY] = collections.defaultdict(bool)

def items(self):
for key, val in super(Scope, self).items():
if key != self.TYPE_CHECKING_ONLY:
yield key, val

def __repr__(self):
scope_cls = self.__class__.__name__
Expand Down Expand Up @@ -1074,8 +1084,6 @@ 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 @@ -1103,6 +1111,7 @@ def addBinding(self, node, value):
# then assume the rebound name is used as a global or within a loop
value.used = self.scope[value.name].used

self.scope[Scope.TYPE_CHECKING_ONLY][value.name] = self._in_type_checking
self.scope[value.name] = value

def _unknown_handler(self, node):
Expand Down Expand Up @@ -1169,7 +1178,8 @@ def handleNodeLoad(self, node):
scope[n.fullName].used = (self.scope, node)
except KeyError:
pass
if n.during_type_checking and not self._in_annotation:
if (self.scope[Scope.TYPE_CHECKING_ONLY][name]
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
Expand Down