summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Stasiak <jakub@stasiak.at>2017-01-22 21:01:45 +0100
committerIan Cordasco <sigmavirus24@users.noreply.github.com>2017-01-22 14:01:45 -0600
commita007c3d7f05b42647ef7d07be7c3afb3bb36ccdb (patch)
tree4b606bed8947a34bfffa09c5261bc115df4027a2
parentf13b1e4c6473f0e91135765452956055e5091fb7 (diff)
downloadpyflakes-a007c3d7f05b42647ef7d07be7c3afb3bb36ccdb.tar.gz
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.
-rw-r--r--pyflakes/checker.py9
-rw-r--r--pyflakes/test/test_other.py7
2 files changed, 7 insertions, 9 deletions
diff --git a/pyflakes/checker.py b/pyflakes/checker.py
index 3e8dc8d..382574e 100644
--- a/pyflakes/checker.py
+++ b/pyflakes/checker.py
@@ -1337,19 +1337,12 @@ class Checker(object):
pass
def ANNASSIGN(self, node):
- """
- Annotated assignments don't have annotations evaluated on function
- scope, hence the custom implementation.
-
- See: PEP 526.
- """
if node.value:
# Only bind the *targets* if the assignment has a value.
# Otherwise it's not really ast.Store and shouldn't silence
# UndefinedLocal warnings.
self.handleNode(node.target, node)
- if not isinstance(self.scope, FunctionScope):
- self.handleNode(node.annotation, node)
+ self.handleNode(node.annotation, node)
if node.value:
# If the assignment has value, handle the *value* now.
self.handleNode(node.value, node)
diff --git a/pyflakes/test/test_other.py b/pyflakes/test/test_other.py
index 6f02001..9c8462e 100644
--- a/pyflakes/test/test_other.py
+++ b/pyflakes/test/test_other.py
@@ -1839,13 +1839,18 @@ class TestAsyncStatements(TestCase):
name: str = 'Bob'
age: int = 18
foo: not_a_real_type = None
- ''', m.UnusedVariable, m.UnusedVariable, m.UnusedVariable)
+ ''', m.UnusedVariable, m.UnusedVariable, m.UnusedVariable, m.UndefinedName)
self.flakes('''
def f():
name: str
print(name)
''', m.UndefinedName)
self.flakes('''
+ from typing import Any
+ def f():
+ a: Any
+ ''')
+ self.flakes('''
foo: not_a_real_type
''', m.UndefinedName)
self.flakes('''