8000 Incorrect inference for types with `__eq__` and no `__hash__` · Issue #18622 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content
8000

Incorrect inference for types with __eq__ and no __hash__ #18622

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

Open
jorenham opened this issue Feb 6, 2025 · 0 comments
Open

Incorrect inference for types with __eq__ and no __hash__ #18622

jorenham opened this issue Feb 6, 2025 · 0 comments
Labels
bug mypy got something wrong topic-runtime-semantics mypy doesn't model runtime semantics correctly

Comments

@jorenham
Copy link
Contributor
jorenham commented Feb 6, 2025

problem

class A:
    def __eq__(self, o: object, /) -> bool:
        return self is o

reveal_type(A().__hash__)  # def () -> builtins.int

https://mypy-play.net/?mypy=latest&python=3.13&flags=strict%2Clocal-partial-types&gist=2b23624b1df772175b70c08e36a6e912

yet

>>> class A:
...     def __eq__(self, o: object, /) -> bool:
...         return self is o
...         
>>> type(A.__hash__)
<class 'NoneType'>

solution

iff. class A, and only class A (i.e. excluding any base classes)

  • implements __eq__, and
  • does not implement __hash__

infer A.__hash__: ClassVar[None]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-runtime-semantics mypy doesn't model runtime semantics correctly
Projects
None yet
Development

No branches or pull requests

2 participants
0