Skip to content
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

Incorrect inference for types with __eq__ and no __hash__ #18622

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

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]

@jorenham jorenham added the bug mypy got something wrong label Feb 6, 2025
@A5rocks A5rocks added the topic-runtime-semantics mypy doesn't model runtime semantics correctly label Feb 7, 2025
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