Deny free type variables in instance attributes coming from constructor args
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
Feature
Detect free type variables as instance attribute annotations, when they appear from constructor with TypeVar in signature without Generic.
Pitch
It would be especially useful for beginners. People often do not understand Generic necessity under some circumstances, though more experienced user may also accidentally forget about this.
This does not deny type variables in constructor arguments, if they do not become part of attributes annotations (say, __init__(self, x: _T, y: _T): do_something(x, y); super().__init__()).
Simple example (playground link):
from typing import TypeVar
_T = TypeVar('_T')
class Foo:
def __init__(self, foo: _T, bar: _T):
self.foo = foo
self.bar = bar
f = Foo(1, 1)
reveal_type(f.foo) # N: Revealed type is "_T`-1"
mypy already denies another scenario, which is essentially the same:
...
class Bar:
x: _T # E: Type variable "__main__._T" is unbound [valid-type]
results in
main.py:15: error: Type variable "__main__._T" is unbound [valid-type]
main.py:15: note: (Hint: Use "Generic[_T]" or "Protocol[_T]" base class to bind "_T" inside a class)
main.py:15: note: (Hint: Use "_T" in function signature to bind "_T" inside a function)
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 reproducer mypy-play collegato e confrontalo con la diagnostica esistente per i TypeVar non associati negli attributi di classe. Traccia come le assegnazioni nel costruttore diventano annotazioni degli attributi di istanza; il lavoro è completo quando lì vengono segnalati i TypeVar non associati, consentendo al contempo i TypeVar usati solo negli argomenti del costruttore.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers, devtools
- Tipo di issue
- Funzionalità
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100