Skip to content

Commit 43541ee

Browse files
authored
Fix annotation clobbering __all__ (#606)
1 parent 650efb9 commit 43541ee

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

pyflakes/checker.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,10 @@ def addBinding(self, node, value):
11401140
# then assume the rebound name is used as a global or within a loop
11411141
value.used = self.scope[value.name].used
11421142

1143-
self.scope[value.name] = value
1143+
# don't treat annotations as assignments if there is an existing value
1144+
# in scope
1145+
if value.name not in self.scope or not isinstance(value, Annotation):
1146+
self.scope[value.name] = value
11441147

11451148
def _unknown_handler(self, node):
11461149
# this environment variable configures whether to error on unknown

pyflakes/test/test_type_annotations.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,19 @@ def f(t: T): pass
335335
def g(t: 'T'): pass
336336
''')
337337

338+
@skipIf(version_info < (3, 6), 'new in Python 3.6')
339+
def test_type_annotation_clobbers_all(self):
340+
self.flakes('''\
341+
from typing import TYPE_CHECKING, List
342+
343+
from y import z
344+
345+
if not TYPE_CHECKING:
346+
__all__ = ("z",)
347+
else:
348+
__all__: List[str]
349+
''')
350+
338351
def test_typeCommentsMarkImportsAsUsed(self):
339352
self.flakes("""
340353
from mod import A, B, C, D, E, F, G

0 commit comments

Comments
 (0)