Incorrect inference for types with `__eq__` and no `__hash__`
Open
Nobody has claimed this yet.
bug
topic-runtime-semantics
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
problem
class A:
def __eq__(self, o: object, /) -> bool:
return self is o
reveal_type(A().__hash__) # def () -> builtins.int
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]
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No source file or test is named. Reproduce the example from the mypy-play link, then locate the type inference entry point for eq and hash. Done means a class defining eq without its own hash is inferred with hash: ClassVar[None], without using base-class definitions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100