python / python/mypy

Incorrect type parameter scope inference with nested classes

Aperta
#21,210 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

In a class whose body is nested in another, mypy doesn't recognize the type parameters of the outer class as bound in the inner.

To Reproduce

# test_mypy.py
class Foo[T]:
    class Bar[R]:
        def foobar(self, t: T, r: R, /) -> None: ...
        print(T, R)

Output of program

T R

Note that there is no NameError being thrown. The names T and R are indeed both bound in the scope of the definition of R.

Actual Behavior

test_mypy.py:3: error: Name "T" is not defined  [name-defined]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used:
  • Command used: mypy test_mypy.py
  • Mypy configuration options from mypy.ini (and other config files): None relevant
  • Python version used: 3.12, 3.13, 3.14

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.

Direzione di ricerca

Inizia eseguendo mypy test_mypy.py con l'esempio annidato Foo[T] e Bar[R] riportato nel rapporto, quindi traccia la gestione dell'ambito dei parametri di tipo per i corpi di classi annidate. Aggiungi un test di regressione che mostri che il T esterno viene riconosciuto insieme all'R interno e conferma che l'errore name-defined non compare più.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
55/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.