Skip to content

Commit a007c3d

Browse files
jstasiaksigmavirus24
authored andcommitted
Process function scope variable annotations (#88)
Even though variable annotations in function scope aren't evaluated at runtime it's still useful for static analysis tools to process them and catch some issues (and not report some things that aren't issues). Let's take the following code: from typing import Any def fun(): a: Any Previously pyflakes would report Any to be unused: test.py:1: 'typing.Any' imported but unused With this patch it's no longer the case.
1 parent f13b1e4 commit a007c3d

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

pyflakes/checker.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,19 +1337,12 @@ def EXCEPTHANDLER(self, node):
13371337
pass
13381338

13391339
def ANNASSIGN(self, node):
1340-
"""
1341-
Annotated assignments don't have annotations evaluated on function
1342-
scope, hence the custom implementation.
1343-
1344-
See: PEP 526.
1345-
"""
13461340
if node.value:
13471341
# Only bind the *targets* if the assignment has a value.
13481342
# Otherwise it's not really ast.Store and shouldn't silence
13491343
# UndefinedLocal warnings.
13501344
self.handleNode(node.target, node)
1351-
if not isinstance(self.scope, FunctionScope):
1352-
self.handleNode(node.annotation, node)
1345+
self.handleNode(node.annotation, node)
13531346
if node.value:
13541347
# If the assignment has value, handle the *value* now.
13551348
self.handleNode(node.value, node)

pyflakes/test/test_other.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1839,13 +1839,18 @@ def f():
18391839
name: str = 'Bob'
18401840
age: int = 18
18411841
foo: not_a_real_type = None
1842-
''', m.UnusedVariable, m.UnusedVariable, m.UnusedVariable)
1842+
''', m.UnusedVariable, m.UnusedVariable, m.UnusedVariable, m.UndefinedName)
18431843
self.flakes('''
18441844
def f():
18451845
name: str
18461846
print(name)
18471847
''', m.UndefinedName)
18481848
self.flakes('''
1849+
from typing import Any
1850+
def f():
1851+
a: Any
1852+
''')
1853+
self.flakes('''
18491854
foo: not_a_real_type
18501855
''', m.UndefinedName)
18511856
self.flakes('''

0 commit comments

Comments
 (0)