Make each assignment to define a distinct variable with independent type
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Lies zunächst die verwandten Issues #6233 und #6232 und sieh dir anschließend die im Issue erwähnte Prototypimplementierung an. Die vorgeschlagene Arbeit ist ein verallgemeinerter Durchlauf zur Umbenennung von Variablen unter Verwendung unterschiedlicher interner Variablen und Phi-Knoten, wobei die Kompatibilität mit der aktuellen Semantik das Ziel der Fertigstellung ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100