Allow Final attributes declared in class body if initialized in ctor
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
There is currently a safety check around TypeVar-dependent Final attributes declared in the class body that I believe we can loosen when the variable is assigned in the ctor. This should work analogously to non-TypeVar-dependent final attributes declared in the class body, which also require an assignment in the ctor (otherwise they emit "Final name must be initialized with a value").
- Are you reporting a bug, or opening a feature request? feature request
- Please insert below the code you are checking with mypy,
or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.from typing import Final, Generic, TypeVar WidgetT = TypeVar('WidgetT', bound='Widget') class Widget: pass class WidgetHolder(Generic[WidgetT]): widget: Final[WidgetT] def __init__(self, widget: WidgetT) -> None: self.widget = widget - What is the actual behavior/output?
final.py: note: In class "WidgetHolder": final.py:13:5: error: Final name declared in class body cannot depend on type variables [misc] widget: Final[WidgetT] ^
In case anyone comes across this issue, the workaround is currently to not declare in the class body, but declare in the ctor with self.widget: Final[WidgetT] = widget instead. But just because a workaround exists doesn't mean we shouldn't try to fix this.
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 eseguendo mypy sulla riproduzione fornita in final.py e ispeziona i controlli esistenti per gli attributi Final dichiarati nel corpo di una classe, confrontandoli con gli attributi Final che non dipendono da TypeVar. Il lavoro è completo quando la riproduzione supera il controllo dei tipi se l’attributo viene assegnato in init, mentre l’errore esistente rimane per le dichiarazioni che non vengono inizializzate lì.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Funzionalità
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 38/100