Allow method assignment in body of type extending typing.NamedTuple
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
this currently works for non-namedtuple classes (both runtime and mypy-time):
class C:
x: int
__hash__ = object.__hash__
and it works for typing.NamedTuple at runtime:
from typing import NamedTuple
class C(NamedTuple):
x: int
__hash__ = object.__hash__
$ python3 -i t2.py
>>> c = C(1)
>>> hash(c)
-9223363269847169460
>>> object.__hash__(c)
-9223363269847169460
>>> tuple.__hash__(c)
3430019387558
but fails at type-checking time:
$ mypy --version && python --version --version
mypy 0.770
Python 3.6.8 (default, Oct 7 2019, 12:59:55)
[GCC 8.3.0]
$ mypy t2.py
t2.py:7: error: NamedTuple field name cannot start with an underscore: __hash__
t2.py:7: error: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]"
Found 2 errors in 1 file (checked 1 source file)
concrete usecase: __hash__ is in the critical path (~13%) for some code I'm writing and the extra layer of function which calls into object.__hash__'s overhead is significant:
# mypy is ok with this
from typing import NamedTuple
class C(NamedTuple):
x: int
def __hash__(self) -> int:
return object.__hash__(self)
I'd be happy to hack on this given some pointers :)
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
Reproduziere das Problem mit den Beispielen in t2.py und führe mypy für die Datei aus, um die beiden Diagnosen für hash zu bestätigen. Verfolge nach, wie NamedTuple-Klassenkörper Felder von Methodenzuweisungen unterscheiden, und sorge dann dafür, dass die Zuweisung die Typprüfung besteht, während die Validierung ungültiger Felddeklarationen erhalten bleibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100