Skip to content

Commit ce08c41

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

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
@@ -1994,9 +1994,17 @@ def IF(self, node):
19941994
prev = self._in_type_checking
19951995
if _is_typing(node.test, 'TYPE_CHECKING', self.scopeStack):
19961996
self._in_type_checking = True
1997-
self.handleChildren(node)
1997+
# Handle the body of this if statement.
1998+
body_nodes = node.body
1999+
if not isinstance(body_nodes, list):
2000+
body_nodes = [body_nodes]
2001+
for body_node in body_nodes:
2002+
self.handleNode(body_node, node)
19982003
self._in_type_checking = prev
19992004

2005+
# Handle the test of the if statement (elif, else).
2006+
self.handleChildren(node, omit=["body"])
2007+
20002008
IFEXP = IF
20012009

20022010
def ASSERT(self, node):

pyflakes/test/test_type_annotations.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,4 +743,24 @@ class X(TypedDict):
743743
744744
class Y(NamedTuple):
745745
y: NamedTuple("v", [("vv", int)])
746+
""")
747+
748+
def test_typing_guard_with_elif_branch(self):
749+
# This test will actually not raise an error, even though it by analysis can
750+
# be shown to have an error (Protocol is not defined outside TYPE_CHECKING).
751+
# Pyflakes currently does not to case analysis.
752+
753+
self.flakes("""
754+
from typing import TYPE_CHECKING
755+
756+
if TYPE_CHECKING:
757+
from typing import Protocol
758+
elif False:
759+
Protocol = object
760+
else:
761+
pass
762+
763+
class C(Protocol):
764+
def f(): # type: () -> int
765+
pass
746766
""")

0 commit comments

Comments
 (0)