Special-casing `__hash__`
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 1.8k
- Forks
- 302
- Ø Merge
- 23 Std.
- Gemergte PRs (30 T.)
- 8
Beschreibung
The concept of "hashable" and "unhashable" classes has been a bit of the sore point in the past. typeshed uses __hash__: ClassVar[None] to mark classes as non-hashable, although that requires a # type: ignore[assignment] annotation. I think this is special-cased by at least some type checkers for marking classes as unhashable.
Except for some wordage in the dataclasses section, the typing spec is silent about hashability. Since I believe that this needs special-casing by type checkers, it should be added to the typing spec. A raw idea:
- Classes are hashable by default (due to
object.__hash__()being implemented and typeshed having a type corresponding type annotation.) - To mark a class as non-hashable, use
__hash__: ClassVar[None]. We could alternatively use something simpler such as__hash__ = None. I believe we used to use that, but I don't know why it's changed. - To mark a class as hashable, use
def __hash__(self, ...) -> ..., usuallydef __hash__(self) -> int: .... - Sub-classes inherit their parent's hashability, unless overridden.
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
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 Typing-Spezifikation zu lesen, insbesondere den bestehenden Abschnitt zu Dataclasses, und sieh dir die Diskussion und die vorgeschlagenen Alternativen in diesem Issue an. Definiere die vereinbarten Regeln für die standardmäßige Hashbarkeit, __hash__: ClassVar[None] oder __hash__ = None, explizite __hash__-Methoden und Vererbung; abgeschlossen bedeutet, dass die Typing-Spezifikation das akzeptierte Verhalten dokumentiert.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- documentation
- Issue-Typ
- Dokumentation
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Ruhig
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100