dataclasses with generated __hash__ subclassing Hashable are considered abstract
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
Dataclasses that are hashable (e.g. frozen=True) that extend any abc/protocol requiring __hash__ (e.g. collections.abc.Hashable) is considered abstract and gives an error when trying to be instantiated.
In addition to frozen=True/Hashable, you also get the same error with unsafe_hash=True, as well as using your own protocol or abc that requires __hash__.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=390504f01055e489b12b388eec1de256
from dataclasses import dataclass
from collections.abc import Hashable
@dataclass(frozen=True)
class A:
a: int
@dataclass(frozen=True)
class B(Hashable):
a: int
a: Hashable = A(1) # ok
b = B(1) # error: Cannot instantiate abstract class "A" with abstract attribute "__hash__" [abstract]
print(hash(b)) # prints -6644214454873602895
Expected Behavior
This works at runtime -- running the script prints the hash, demonstrating that it has __hash__ defined.
Actual Behavior
main.py:16: error: Cannot instantiate abstract class "B" with abstract attribute "__hash__" [abstract]
Your Environment
- Mypy version used: 1.15.0
- Mypy command-line flags: N/A
- Mypy configuration options from
mypy.ini(and other config files): N/A - Python version used: 3.12
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
Start by running the linked mypy-play reproduction with Python 3.12 and mypy 1.15.0. Trace how dataclasses with frozen=True or unsafe_hash=True are treated when a subclass requires hash, then add regression coverage showing that B(1) is accepted without an abstract-class error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100