Generic class clone method using property variable for type arg produces name-defined error
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
My Python code is running fine for what I want it to do, but I've had to silence this mypy error in this case, where it does not appear to be correct. A property is used to access the __orig_class__ variable.
To reproduce:
mypy playground - https://mypy-play.net/?mypy=1.10.0&python=3.12&gist=063c6160fe6d1bf37973f47906a6d997
from __future__ import annotations
from typing import Generic, TypeVar, get_args
T = TypeVar("T", bound=int)
class Foo(Generic[T]):
@property
def t(self) -> type[T]:
return get_args(self.__orig_class__)[0] # Get the type arg, (int)
def clone(self) -> Foo[T]:
return Foo[self.t]() # test.py:12: error: Name "self.t" is not defined [name-defined]
def no_complaint(self) -> None:
print(self.t("1"))
foo = Foo[int]()
foo.no_complaint() # `1`
r = foo.clone()
r.no_complaint() # `1`
Expected behaviour
Mypy reports no errors, python code runs correctly.
Actual behaviour
Python code runs correctly.
Mypy returns
test.py:12: error: Name "self.t" is not defined [name-defined]
Environment
Base install with:
mypy==1.10.0
python==3.11.4
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con il mypy playground collegato e con l’esempio riportato per Python 3.12 e mypy 1.10.0 in test.py, quindi traccia come viene prodotto il diagnostico name-defined per self.t all’interno di Foo.clone. Il lavoro è completato quando la riproduzione non segnala errori e mantiene il comportamento mostrato a runtime.
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à
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100