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 @@ -1994,9 +1994,17 @@ def IF(self, node):
1994
1994
prev = self ._in_type_checking
1995
1995
if _is_typing (node .test , 'TYPE_CHECKING' , self .scopeStack ):
1996
1996
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 )
1998
2003
self ._in_type_checking = prev
1999
2004
2005
+ # Handle the test of the if statement (elif, else).
2006
+ self .handleChildren (node , omit = ["body" ])
2007
+
2000
2008
IFEXP = IF
2001
2009
2002
2010
def ASSERT (self , node ):
Original file line number Diff line number Diff line change @@ -743,4 +743,24 @@ class X(TypedDict):
743
743
744
744
class Y(NamedTuple):
745
745
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
746
766
""" )
You can’t perform that action at this time.
0 commit comments