3232)
3333from mypy .checkpattern import PatternChecker
3434from mypy .constraints import SUPERTYPE_OF
35- from mypy .erasetype import erase_type , erase_typevars , remove_instance_last_known_values
35+ from mypy .erasetype import (
36+ erase_type ,
37+ erase_typevars ,
38+ remove_instance_last_known_values ,
39+ shallow_erase_type_for_equality ,
40+ )
3641from mypy .errorcodes import TYPE_VAR , UNUSED_AWAITABLE , UNUSED_COROUTINE , ErrorCode
3742from mypy .errors import (
3843 ErrorInfo ,
4550from mypy .expandtype import expand_type
4651from mypy .literals import Key , extract_var_from_literal_hash , literal , literal_hash
4752from mypy .maptype import map_instance_to_supertype
48- from mypy .meet import is_overlapping_erased_types , is_overlapping_types , meet_types
53+ from mypy .meet import is_overlapping_types , meet_types
4954from mypy .message_registry import ErrorMessage
5055from mypy .messages import (
5156 SUGGESTED_TEST_FIXTURES ,
@@ -6540,19 +6545,6 @@ def comparison_type_narrowing_helper(self, node: ComparisonExpr) -> tuple[TypeMa
65406545 narrowable_indices = {0 },
65416546 )
65426547
6543- # We only try and narrow away 'None' for now
6544- if (
6545- not is_unreachable_map (if_map )
6546- and is_overlapping_none (item_type )
6547- and not is_overlapping_none (collection_item_type )
6548- and not (
6549- isinstance (collection_item_type , Instance )
6550- and collection_item_type .type .fullname == "builtins.object"
6551- )
6552- and is_overlapping_erased_types (item_type , collection_item_type )
6553- ):
6554- if_map [operands [left_index ]] = remove_optional (item_type )
6555-
65566548 if right_index in narrowable_operand_index_to_hash :
65576549 if_type , else_type = self .conditional_types_for_iterable (
65586550 item_type , iterable_type
@@ -6676,6 +6668,9 @@ def narrow_type_by_identity_equality(
66766668 target_type = operand_types [j ]
66776669 if should_coerce_literals :
66786670 target_type = coerce_to_literal (target_type )
6671+ # Type A[T1] could compare equal to A[T2] even if T1 is disjoint from T2
6672+ # e.g. cast(list[int], []) == cast(list[str], [])
6673+ target_type = shallow_erase_type_for_equality (target_type )
66796674
66806675 if (
66816676 # See comments in ambiguous_enum_equality_keys
@@ -6689,7 +6684,7 @@ def narrow_type_by_identity_equality(
66896684 if_map , else_map = conditional_types_to_typemaps (
66906685 operands [i ], * conditional_types (expr_type , [target ])
66916686 )
6692- if is_target_for_value_narrowing (get_proper_type ( target_type ) ):
6687+ if is_target_for_value_narrowing (target_type ):
66936688 all_if_maps .append (if_map )
66946689 all_else_maps .append (else_map )
66956690 else :
@@ -6758,13 +6753,15 @@ def narrow_type_by_identity_equality(
67586753 target_type = operand_types [j ]
67596754 if should_coerce_literals :
67606755 target_type = coerce_to_literal (target_type )
6756+ target_type = shallow_erase_type_for_equality (target_type )
6757+
67616758 target = TypeRange (target_type , is_upper_bound = False )
67626759
67636760 if_map , else_map = conditional_types_to_typemaps (
67646761 operands [i ], * conditional_types (expr_type , [target ], default = expr_type )
67656762 )
67666763 or_if_maps .append (if_map )
6767- if is_target_for_value_narrowing (get_proper_type ( target_type ) ):
6764+ if is_target_for_value_narrowing (target_type ):
67686765 or_else_maps .append (else_map )
67696766
67706767 all_if_maps .append (reduce_or_conditional_type_maps (or_if_maps ))
@@ -8609,13 +8606,7 @@ def reduce_and_conditional_type_maps(ms: list[TypeMap], *, use_meet: bool) -> Ty
86098606 return result
86108607
86118608
8612- BUILTINS_CUSTOM_EQ_CHECKS : Final = {
8613- "builtins.bytearray" ,
8614- "builtins.memoryview" ,
8615- "builtins.list" ,
8616- "builtins.dict" ,
8617- "builtins.set" ,
8618- }
8609+ BUILTINS_CUSTOM_EQ_CHECKS : Final = {"builtins.bytearray" , "builtins.memoryview" }
86198610
86208611
86218612def has_custom_eq_checks (t : Type ) -> bool :
0 commit comments