python / python/mypy

`NamedTuple`s cannot have nested classes

Aperta
#15,775 1 commento 0 reazioni 1 assegnatario Vedi su GitHub

@sobolevn ci sta già lavorando.

Dal 29/7/2023.

bug topic-named-tuple
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.