File tree Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -1852,9 +1852,17 @@ def IF(self, node):
1852
1852
prev = self ._in_type_checking
1853
1853
if _is_typing (node .test , 'TYPE_CHECKING' , self .scopeStack ):
1854
1854
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 )
1856
1861
self ._in_type_checking = prev
1857
1862
1863
+ # Handle the test of the if statement (elif, else).
1864
+ self .handleChildren (node , omit = ["body" ])
1865
+
1858
1866
IFEXP = IF
1859
1867
1860
1868
def ASSERT (self , node ):
Original file line number Diff line number Diff line change @@ -596,3 +596,23 @@ class C(Protocol):
596
596
def f(): # type: () -> int
597
597
pass
598
598
""" )
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
+ """ )
You can’t perform that action at this time.
0 commit comments