Make each assignment to define a distinct variable with independent type

Abierto
#18,516 4 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
25/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
python
Área
compilers

Línea de trabajo

Empieza leyendo los issues relacionados #6233 y #6232; después, revisa la implementación del prototipo mencionada en el issue. El trabajo propuesto consiste en una pasada generalizada de renombrado de variables que utiliza variables internas distintas y nodos phi, con la compatibilidad con la semántica actual como objetivo de finalización.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

feature priority-0-high

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.append call).
  • 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.
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Merge medio
1 d 18 h
PR fusionados (30 d)
54

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de python/mypy

Todos los issues de python/mypy

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.