Make each assignment to define a distinct variable with independent type
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
These long-standing issues can be solved by generalizing the variable renaming pass that is used by --allow-redefinition:
- #6233
- #6232
Each assignment to a name would generate a separate internal variable. We will use "phi nodes" to merge these variables when different control flow paths assign to different variants. Here is a simple example:
def f() -> None:
if c():
x = 0
else:
x = ""
reveal_type(x)
The new variable renaming pass would produce a new AST that resembles this program (note that phi(...) is a new special AST node type and not a function call):
def f() -> None:
if c():
x = 0
else:
x' = ""
x'' = phi(x, x')
reveal_type(x'') # int | str
This resembles the static single assignment form (SSA) used by many compilers, but probably would not conform to it 100% due to various practical reasons.
I'm working on a prototype implementation.
I hope that we can make this sufficiently compatible with the current semantics so that we can enable it by default (in mypy 2.0, possibly).
An alternative way to provide similar functionality would be to infer variable types from multiple assignments, similar to what already happens if a variable is declared as x: object. The renaming approach has a few notable benefits:
- It can (more) easily support partial types (e.g. inferring a list item type from an
x.appendcall). - It's a generalization of how we've already implemented
--allow-redefinition. - It should make it easier to generate efficient code in mypyc.
- The implementation will mostly be a new renaming pass, so it won't make other parts of mypy much more complicated (though mypyc needs changes).
- The conditional type binder has some tech debt and I'm not excited about making it even more complicated, which would be the case if we'd use the alternative approach.
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 leggendo le issue correlate #6233 e #6232, quindi esamina l'implementazione del prototipo menzionata nell'issue. Il lavoro proposto consiste in un pass generalizzato di ridenominazione delle variabili che utilizza variabili interne distinte e nodi phi, con la compatibilità con la semantica attuale come obiettivo del completamento.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100