Skip to content

Commit a993d91

Browse files
committed
Fix: elif and else branches should not be affected.
1 parent f5657ba commit a993d91

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

pyflakes/checker.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1852,9 +1852,17 @@ def IF(self, node):
18521852
prev = self._in_type_checking
18531853
if _is_typing(node.test, 'TYPE_CHECKING', self.scopeStack):
18541854
self._in_type_checking = True
1855-
self.handleChildren(node)
1855+
# Handle the body of this if statement.
1856+
body_nodes = node.body
1857+
if not isinstance(body_nodes, list):
1858+
body_nodes = [body_nodes]
1859+
for body_node in body_nodes:
1860+
self.handleNode(body_node, node)
18561861
self._in_type_checking = prev
18571862

1863+
# Handle the test of the if statement (elif, else).
1864+
self.handleChildren(node, omit=["body"])
1865+
18581866
IFEXP = IF
18591867

18601868
def ASSERT(self, node):

pyflakes/test/test_type_annotations.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,3 +596,23 @@ class C(Protocol):
596596
def f(): # type: () -> int
597597
pass
598598
""")
599+
600+
def test_typing_guard_with_elif_branch(self):
601+
# This test will actually not raise an error, even though it by analysis can
602+
# be shown to have an error (Protocol is not defined outside TYPE_CHECKING).
603+
# Pyflakes currently does not to case analysis.
604+
605+
self.flakes("""
606+
from typing import TYPE_CHECKING
607+
608+
if TYPE_CHECKING:
609+
from typing import Protocol
610+
elif False:
611+
Protocol = object
612+
else:
613+
pass
614+
615+
class C(Protocol):
616+
def f(): # type: () -> int
617+
pass
618+
""")

0 commit comments

Comments
 (0)