@@ -6829,8 +6829,10 @@ def narrow_type_by_identity_equality(
68296829 continue
68306830 expr = operands [j ]
68316831
6832- current_type_range = self .get_isinstance_type (expr )
6833- if current_type_range is not None :
6832+ current_type_range = self .get_isinstance_type (expr , flatten_tuples = False )
6833+ if current_type_range is None :
6834+ current_type_range = []
6835+ else :
68346836 target_type = make_simplified_union ([tr .item for tr in current_type_range ])
68356837 if isinstance (target_type , AnyType ):
68366838 # Avoid widening to Any for checks like `type(x) is type(y: Any)`.
@@ -7921,22 +7923,29 @@ def is_writable_attribute(self, node: Node) -> bool:
79217923 return first_item .var .is_settable_property
79227924 return False
79237925
7924- def get_isinstance_type (self , expr : Expression ) -> list [TypeRange ] | None :
7926+ def get_isinstance_type (
7927+ self , expr : Expression , flatten_tuples : bool = True
7928+ ) -> list [TypeRange ] | None :
79257929 """Get the type(s) resulting from an isinstance check.
79267930
79277931 Returns an empty list for isinstance(x, ()).
79287932 """
79297933 if isinstance (expr , OpExpr ) and expr .op == "|" :
7930- left = self .get_isinstance_type (expr .left )
7934+ left = self .get_isinstance_type (expr .left , flatten_tuples = False )
79317935 if left is None and is_literal_none (expr .left ):
79327936 left = [TypeRange (NoneType (), is_upper_bound = False )]
7933- right = self .get_isinstance_type (expr .right )
7937+ right = self .get_isinstance_type (expr .right , flatten_tuples = False )
79347938 if right is None and is_literal_none (expr .right ):
79357939 right = [TypeRange (NoneType (), is_upper_bound = False )]
79367940 if left is None or right is None :
79377941 return None
79387942 return left + right
7939- all_types = get_proper_types (flatten_types (self .lookup_type (expr )))
7943+
7944+ if flatten_tuples :
7945+ all_types = get_proper_types (flatten_types_if_tuple (self .lookup_type (expr )))
7946+ else :
7947+ all_types = [get_proper_type (self .lookup_type (expr ))]
7948+
79407949 types : list [TypeRange ] = []
79417950 for typ in all_types :
79427951 if isinstance (typ , FunctionLike ) and typ .is_type_obj ():
@@ -8640,11 +8649,11 @@ def flatten(t: Expression) -> list[Expression]:
86408649 return [t ]
86418650
86428651
8643- def flatten_types (t : Type ) -> list [Type ]:
8652+ def flatten_types_if_tuple (t : Type ) -> list [Type ]:
86448653 """Flatten a nested sequence of tuples into one list of nodes."""
86458654 t = get_proper_type (t )
86468655 if isinstance (t , TupleType ):
8647- return [b for a in t .items for b in flatten_types (a )]
8656+ return [b for a in t .items for b in flatten_types_if_tuple (a )]
86488657 elif is_named_instance (t , "builtins.tuple" ):
86498658 return [t .args [0 ]]
86508659 else :
0 commit comments