Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 4 additions & 17 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -8924,24 +8924,9 @@ def reduce_and_conditional_type_maps(ms: list[TypeMap], *, use_meet: bool) -> Ty
return result


BUILTINS_CUSTOM_EQ_CHECKS: Final = {
"builtins.frozenset",
"_collections_abc.dict_keys",
"_collections_abc.dict_items",
}


def has_custom_eq_checks(t: Type) -> bool:
return (
custom_special_method(t, "__eq__", check_all=False)
or custom_special_method(t, "__ne__", check_all=False)
# custom_special_method has special casing for builtins.* and typing.* that make the
# above always return False. Some builtin collections still have equality behavior that
# crosses nominal type boundaries and isn't captured by VALUE_EQUALITY_TYPE_DOMAINS.
or (
isinstance(pt := get_proper_type(t), Instance)
and pt.type.fullname in BUILTINS_CUSTOM_EQ_CHECKS
)
return custom_special_method(t, "__eq__", check_all=False) or custom_special_method(
t, "__ne__", check_all=False
)


Expand Down Expand Up @@ -9731,6 +9716,8 @@ def visit_starred_pattern(self, p: StarredPattern) -> None:
"builtins.bytes": "builtins.bytes",
"builtins.bytearray": "builtins.bytes",
"builtins.memoryview": "builtins.bytes",
"typing.Mapping": "typing.Mapping",
"typing.AbstractSet": "typing.AbstractSet",
}

VALUE_EQUALITY_DOMAINS: Final = {**OPEN_VALUE_EQUALITY_DOMAINS, **CLOSED_VALUE_EQUALITY_DOMAINS}
Expand Down
56 changes: 56 additions & 0 deletions test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -2212,3 +2212,59 @@ d3: A = d1 | d2
reveal_type(d1 | d2)
[out]
_testDictOrUnionEdgeCases.py:10: note: Revealed type is "dict[str, dict[str, ... | int] | int]"

[case testNarrowingMappingAndAbstractSet]
# flags: --strict-equality --warn-unreachable
from typing import Mapping, AbstractSet

def f1(x: Mapping[str, int], y: dict[str, int]) -> None:
if isinstance(x, dict):
reveal_type(x)

if x == y:
reveal_type(x)
reveal_type(y)

if x == {}:
reveal_type(x)

def f2(x: AbstractSet[int], y: set[int]) -> None:
if isinstance(x, set):
reveal_type(x)

if x == y:
reveal_type(x)
reveal_type(y)

if x == set():
reveal_type(x)

def f3(x: frozenset[int], y: set[int]):
if x == y:
reveal_type(x)
reveal_type(y)

def f4(x: dict[str, int], y: set[str]):
if x.keys() == y:
reveal_type(x)
reveal_type(y)

z = x.keys()
if z == y:
reveal_type(z)
reveal_type(y)
[out]
_testNarrowingMappingAndAbstractSet.py:6: note: Revealed type is "dict[Any, Any]"
_testNarrowingMappingAndAbstractSet.py:9: note: Revealed type is "typing.Mapping[str, int]"
_testNarrowingMappingAndAbstractSet.py:10: note: Revealed type is "dict[str, int]"
_testNarrowingMappingAndAbstractSet.py:13: note: Revealed type is "typing.Mapping[str, int]"
_testNarrowingMappingAndAbstractSet.py:17: note: Revealed type is "set[Any]"
_testNarrowingMappingAndAbstractSet.py:20: note: Revealed type is "typing.AbstractSet[int]"
_testNarrowingMappingAndAbstractSet.py:21: note: Revealed type is "set[int]"
_testNarrowingMappingAndAbstractSet.py:24: note: Revealed type is "typing.AbstractSet[int]"
_testNarrowingMappingAndAbstractSet.py:28: note: Revealed type is "frozenset[int]"
_testNarrowingMappingAndAbstractSet.py:29: note: Revealed type is "set[int]"
_testNarrowingMappingAndAbstractSet.py:33: note: Revealed type is "dict[str, int]"
_testNarrowingMappingAndAbstractSet.py:34: note: Revealed type is "set[str]"
_testNarrowingMappingAndAbstractSet.py:38: note: Revealed type is "_collections_abc.dict_keys[str, int]"
_testNarrowingMappingAndAbstractSet.py:39: note: Revealed type is "set[str]"
Loading