Skip to content

Commit

Permalink
fix: support recursive models in local scope
Browse files Browse the repository at this point in the history
closes #567
  • Loading branch information
PrettyWood committed Dec 25, 2020
1 parent f2a4681 commit c3c0d35
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
7 changes: 1 addition & 6 deletions pyflakes/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,6 @@ class Checker(object):
offset = None
traceTree = False
_in_annotation = AnnotationState.NONE
_in_deferred = False

builtIns = set(builtin_vars).union(_MAGIC_GLOBALS)
_customBuiltIns = os.environ.get('PYFLAKES_BUILTINS')
Expand Down Expand Up @@ -898,7 +897,6 @@ def __init__(self, tree, filename='(none)', builtins=None,
for builtin in self.builtIns:
self.addBinding(None, Builtin(builtin))
self.handleChildren(tree)
self._in_deferred = True
self.runDeferred(self._deferredFunctions)
# Set _deferredFunctions to None so that deferFunction will fail
# noisily if called after we've run through the deferred functions.
Expand Down Expand Up @@ -1803,10 +1801,7 @@ def STR(self, node):
node.col_offset,
messages.ForwardAnnotationSyntaxError,
)
if self._in_deferred:
fn()
else:
self.deferFunction(fn)
self.deferFunction(fn)

if PY38_PLUS:
def CONSTANT(self, node):
Expand Down
19 changes: 19 additions & 0 deletions pyflakes/test/test_type_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,3 +667,22 @@ class C(Protocol):
def f(): # type: () -> int
pass
""")

@skipIf(version_info < (3, 6), 'new in Python 3.6')
def test_recursive_global_scope(self):
self.flakes("""
from typing import Optional, Tuple
class NestedTuple:
x: Tuple[int, Optional['NestedTuple']]
""")

@skipIf(version_info < (3, 6), 'new in Python 3.6')
def test_recursive_local_scope(self):
self.flakes("""
from typing import Optional, Tuple
def test():
class NestedTuple:
x: Tuple[int, Optional['NestedTuple']]
""")

0 comments on commit c3c0d35

Please sign in to comment.