diff options
author | Anthony Sottile <asottile@umich.edu> | 2021-01-05 08:04:38 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-05 08:04:38 -0800 |
commit | 43541ee1dd394ec4691625e7295f701b3b073dca (patch) | |
tree | 6742db472b00ee5f93d492007391f6221485bc4e | |
parent | 650efb92fd27ae60ec08a70f2ac1afbea37ac3e3 (diff) | |
download | pyflakes-43541ee1dd394ec4691625e7295f701b3b073dca.tar.gz |
Fix annotation clobbering __all__ (#606)
-rw-r--r-- | pyflakes/checker.py | 5 | ||||
-rw-r--r-- | pyflakes/test/test_type_annotations.py | 13 |
2 files changed, 17 insertions, 1 deletions
diff --git a/pyflakes/checker.py b/pyflakes/checker.py index 38f8f73..215bd31 100644 --- a/pyflakes/checker.py +++ b/pyflakes/checker.py @@ -1140,7 +1140,10 @@ class Checker(object): # then assume the rebound name is used as a global or within a loop value.used = self.scope[value.name].used - self.scope[value.name] = value + # don't treat annotations as assignments if there is an existing value + # in scope + if value.name not in self.scope or not isinstance(value, Annotation): + self.scope[value.name] = value def _unknown_handler(self, node): # this environment variable configures whether to error on unknown diff --git a/pyflakes/test/test_type_annotations.py b/pyflakes/test/test_type_annotations.py index 409238b..835d8d9 100644 --- a/pyflakes/test/test_type_annotations.py +++ b/pyflakes/test/test_type_annotations.py @@ -335,6 +335,19 @@ class TestTypeAnnotations(TestCase): def g(t: 'T'): pass ''') + @skipIf(version_info < (3, 6), 'new in Python 3.6') + def test_type_annotation_clobbers_all(self): + self.flakes('''\ + from typing import TYPE_CHECKING, List + + from y import z + + if not TYPE_CHECKING: + __all__ = ("z",) + else: + __all__: List[str] + ''') + def test_typeCommentsMarkImportsAsUsed(self): self.flakes(""" from mod import A, B, C, D, E, F, G |