Skip to content

Commit ffd48e0

Browse files
committed
.
1 parent 79e9c78 commit ffd48e0

4 files changed

Lines changed: 29 additions & 36 deletions

File tree

mypy/checker.py

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
from mypy.expandtype import expand_type
4646
from mypy.literals import Key, extract_var_from_literal_hash, literal, literal_hash
4747
from mypy.maptype import map_instance_to_supertype
48-
from mypy.meet import is_overlapping_erased_types, is_overlapping_types, meet_types
48+
from mypy.meet import is_overlapping_types, meet_types
4949
from mypy.message_registry import ErrorMessage
5050
from mypy.messages import (
5151
SUGGESTED_TEST_FIXTURES,
@@ -235,7 +235,7 @@
235235
is_literal_type,
236236
is_named_instance,
237237
)
238-
from mypy.types_utils import is_overlapping_none, remove_optional, store_argument_type, strip_type
238+
from mypy.types_utils import store_argument_type, strip_type
239239
from mypy.typetraverser import TypeTraverserVisitor
240240
from mypy.typevars import fill_typevars, fill_typevars_with_any, has_no_typevars
241241
from mypy.util import is_dunder, is_sunder
@@ -6540,19 +6540,6 @@ def comparison_type_narrowing_helper(self, node: ComparisonExpr) -> tuple[TypeMa
65406540
narrowable_indices={0},
65416541
)
65426542

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-
65566543
if right_index in narrowable_operand_index_to_hash:
65576544
if_type, else_type = self.conditional_types_for_iterable(
65586545
item_type, iterable_type
@@ -6676,6 +6663,8 @@ def narrow_type_by_identity_equality(
66766663
target_type = operand_types[j]
66776664
if should_coerce_literals:
66786665
target_type = coerce_to_literal(target_type)
6666+
if isinstance(get_proper_type(target_type), Instance):
6667+
target_type = erase_type(target_type)
66796668

66806669
if (
66816670
# See comments in ambiguous_enum_equality_keys
@@ -8602,13 +8591,7 @@ def reduce_and_conditional_type_maps(ms: list[TypeMap], *, use_meet: bool) -> Ty
86028591
return result
86038592

86048593

8605-
BUILTINS_CUSTOM_EQ_CHECKS: Final = {
8606-
"builtins.bytearray",
8607-
"builtins.memoryview",
8608-
"builtins.list",
8609-
"builtins.dict",
8610-
"builtins.set",
8611-
}
8594+
BUILTINS_CUSTOM_EQ_CHECKS: Final = {"builtins.bytearray", "builtins.memoryview"}
86128595

86138596

86148597
def has_custom_eq_checks(t: Type) -> bool:

mypy/meet.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from collections.abc import Callable
44

55
from mypy import join
6-
from mypy.erasetype import erase_type
76
from mypy.maptype import map_instance_to_supertype
87
from mypy.state import state
98
from mypy.subtypes import (
@@ -657,18 +656,6 @@ def _type_object_overlap(left: Type, right: Type) -> bool:
657656
return False
658657

659658

660-
def is_overlapping_erased_types(
661-
left: Type, right: Type, *, ignore_promotions: bool = False
662-
) -> bool:
663-
"""The same as 'is_overlapping_erased_types', except the types are erased first."""
664-
return is_overlapping_types(
665-
erase_type(left),
666-
erase_type(right),
667-
ignore_promotions=ignore_promotions,
668-
prohibit_none_typevar_overlap=True,
669-
)
670-
671-
672659
def are_typed_dicts_overlapping(
673660
left: TypedDictType, right: TypedDictType, is_overlapping: Callable[[Type, Type], bool]
674661
) -> bool:

test-data/unit/check-narrowing.test

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,27 @@ def f(x: Custom, y: CustomSub):
10491049
reveal_type(y) # N: Revealed type is "__main__.CustomSub"
10501050
[builtins fixtures/tuple.pyi]
10511051

1052+
[case testNarrowingCustomEqualityGeneric]
1053+
# flags: --strict-equality --warn-unreachable
1054+
from __future__ import annotations
1055+
1056+
class Custom:
1057+
def __eq__(self, other: object) -> bool:
1058+
raise
1059+
1060+
class Default: ...
1061+
1062+
def f(x: list[Custom] | Default, y: list[int]):
1063+
if x == y: # E: Non-overlapping equality check (left operand type: "list[Custom] | Default", right operand type: "list[int]")
1064+
reveal_type(x) # N: Revealed type is "builtins.list[__main__.Custom]"
1065+
reveal_type(y) # N: Revealed type is "builtins.list[builtins.int]"
1066+
else:
1067+
reveal_type(x) # N: Revealed type is "builtins.list[__main__.Custom] | __main__.Default"
1068+
reveal_type(y) # N: Revealed type is "builtins.list[builtins.int]"
1069+
1070+
f([], [])
1071+
[builtins fixtures/list.pyi]
1072+
10521073
[case testNarrowingUnreachableCases]
10531074
# flags: --strict-equality --warn-unreachable
10541075
from typing import Literal, Union

test-data/unit/check-tuples.test

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1540,7 +1540,9 @@ class B: pass
15401540

15411541
def f1(possibles: Tuple[int, Tuple[A]], x: Optional[Tuple[B]]):
15421542
if x in possibles:
1543-
reveal_type(x) # N: Revealed type is "tuple[__main__.B]"
1543+
# TODO: this branch is actually unreachable
1544+
# This is an easy fix: https://github.com/python/mypy/pull/20660
1545+
reveal_type(x) # N: Revealed type is "tuple[__main__.B] | None"
15441546
else:
15451547
reveal_type(x) # N: Revealed type is "tuple[__main__.B] | None"
15461548

0 commit comments

Comments
 (0)