dataclasses with generated __hash__ subclassing Hashable are considered abstract
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, die verlinkte mypy-play-Reproduktion mit Python 3.12 und mypy 1.15.0 auszuführen. Verfolge, wie dataclasses mit frozen=True oder unsafe_hash=True behandelt werden, wenn eine Unterklasse hash benötigt, und füge dann eine Regressionstestabdeckung hinzu, die zeigt, dass B(1) ohne einen Fehler wegen einer abstrakten Klasse akzeptiert wird.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 45/100