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