Skip to content

Avoid spurious non-overlapping eq error with metaclass with __eq__ #19220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2025
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
4 changes: 4 additions & 0 deletions mypy/typeops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,10 @@ def custom_special_method(typ: Type, name: str, check_all: bool = False) -> bool
if isinstance(typ, FunctionLike) and typ.is_type_obj():
# Look up __method__ on the metaclass for class objects.
return custom_special_method(typ.fallback, name, check_all)
if isinstance(typ, TypeType) and isinstance(typ.item, Instance):
if typ.item.type.metaclass_type:
# Look up __method__ on the metaclass for class objects.
return custom_special_method(typ.item.type.metaclass_type, name, check_all)
if isinstance(typ, AnyType):
# Avoid false positives in uncertain cases.
return True
Expand Down
8 changes: 8 additions & 0 deletions test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -2172,6 +2172,14 @@ class Custom(metaclass=CustomMeta): ...
Normal == int() # E: Non-overlapping equality check (left operand type: "type[Normal]", right operand type: "int")
Normal == Normal
Custom == int()

n: type[Normal] = Normal
c: type[Custom] = Custom

n == int() # E: Non-overlapping equality check (left operand type: "type[Normal]", right operand type: "int")
n == n
c == int()

[builtins fixtures/bool.pyi]

[case testCustomContainsCheckStrictEquality]
Expand Down