`NamedTuple`s cannot have nested classes
Offen
@sobolevn arbeitet bereits daran.
Seit 29.7.2023.
bug
topic-named-tuple
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Python allows class definitions inside NamedTuple bodies:
>>> from typing import NamedTuple
>>> class A(NamedTuple):
... class B: ...
...
>>> A.B
<class '__main__.A.B'>
>>> A().B
<class '__main__.A.B'>
But, mypy does not allow this:
from typing import NamedTuple
class A(NamedTuple):
class B: ... # E: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]"
reveal_type(A.B) # E: "type[A]" has no attribute "B"
# N: Revealed type is "Any"
I think that this is a bug that needs to be solved, because we don't have any reason not to allow this (my existing patch shows that semanal and typechecker are happy with allowing these classes).
This is the root cause of https://github.com/python/mypy/issues/15752
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.
Bewertung
Dieses Issue wurde noch nicht bewertet.