@@ -3924,10 +3924,12 @@ def find_isinstance_check_helper(self, node: Expression) -> Tuple[TypeMap, TypeM
3924
3924
elif is_false_literal (node ):
3925
3925
return None , {}
3926
3926
elif isinstance (node , CallExpr ):
3927
+ if len (node .args ) == 0 :
3928
+ return {}, {}
3929
+ expr = collapse_walrus (node .args [0 ])
3927
3930
if refers_to_fullname (node .callee , 'builtins.isinstance' ):
3928
3931
if len (node .args ) != 2 : # the error will be reported elsewhere
3929
3932
return {}, {}
3930
- expr = node .args [0 ]
3931
3933
if literal (expr ) == LITERAL_TYPE :
3932
3934
return self .conditional_type_map_with_intersection (
3933
3935
expr ,
@@ -3937,13 +3939,11 @@ def find_isinstance_check_helper(self, node: Expression) -> Tuple[TypeMap, TypeM
3937
3939
elif refers_to_fullname (node .callee , 'builtins.issubclass' ):
3938
3940
if len (node .args ) != 2 : # the error will be reported elsewhere
3939
3941
return {}, {}
3940
- expr = node .args [0 ]
3941
3942
if literal (expr ) == LITERAL_TYPE :
3942
3943
return self .infer_issubclass_maps (node , expr , type_map )
3943
3944
elif refers_to_fullname (node .callee , 'builtins.callable' ):
3944
3945
if len (node .args ) != 1 : # the error will be reported elsewhere
3945
3946
return {}, {}
3946
- expr = node .args [0 ]
3947
3947
if literal (expr ) == LITERAL_TYPE :
3948
3948
vartype = type_map [expr ]
3949
3949
return self .conditional_callable_type_map (expr , vartype )
@@ -3952,7 +3952,7 @@ def find_isinstance_check_helper(self, node: Expression) -> Tuple[TypeMap, TypeM
3952
3952
# narrow their types. (For example, we shouldn't try narrowing the
3953
3953
# types of literal string or enum expressions).
3954
3954
3955
- operands = node .operands
3955
+ operands = [ collapse_walrus ( x ) for x in node .operands ]
3956
3956
operand_types = []
3957
3957
narrowable_operand_index_to_hash = {}
3958
3958
for i , expr in enumerate (operands ):
@@ -5742,3 +5742,14 @@ def has_bool_item(typ: ProperType) -> bool:
5742
5742
return any (is_named_instance (item , 'builtins.bool' )
5743
5743
for item in typ .items )
5744
5744
return False
5745
+
5746
+
5747
+ def collapse_walrus (e : Expression ) -> Expression :
5748
+ """If an expression is an AssignmentExpr, pull out the assignment target.
5749
+
5750
+ We don't make any attempt to pull out all the targets in code like `x := (y := z)`.
5751
+ We could support narrowing those if that sort of code turns out to be common.
5752
+ """
5753
+ if isinstance (e , AssignmentExpr ):
5754
+ return e .target
5755
+ return e
0 commit comments