`unbound-name` not reported after a for loop that contains `break`

Abierto
#5,566 2 comentarios 0 reacciones 1 asignado Ver en GitHub

@1-DARK ya está trabajando en esto.

Desde el 7/11/2026.

  • #5578 de @1-DARK — abierto

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
48/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python, rust

Línea de trabajo

Start by locating Pyrefly’s control-flow and unbound-name analysis, then trace how break affects whether a loop may execute zero times. Reproduce the report with the provided Python example and run the relevant checker tests. Done when the second return is also reported as potentially unbound while existing loop diagnostics still pass.

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

Descripción

scoping-control-flow

If a variable is only assigned inside a for loop, pyrefly correctly warns when it's used after the loop. But as soon as the loop body contains a break, the warning goes away, even though the variable is still unbound when the iterable is empty.

def last_value(values: list[int]) -> int:
    for value in values:
        last = value
    return last  # UnboundLocalError when `values` is empty


def last_before_zero(values: list[int]) -> int:
    for value in values:
        last = value
        if last == 0:
            break
    return last  # UnboundLocalError when `values` is empty


last_before_zero([])

pyrefly check --preset default --python-version 3.14 loop_break.py with pyrefly 1.4.1:

ERROR `last` may be uninitialized [unbound-name]
 --> loop_break.py:4:12
  |
4 |     return last  # UnboundLocalError when `values` is empty
  |            ^^^^

Only line 4 is flagged. I'd expect line 12 as well, since running the file fails there:

UnboundLocalError: cannot access local variable 'last' where it is not associated with a value

For what it's worth, pyright 1.1.414 reports both lines (reportPossiblyUnboundVariable).

Lenguaje dominante
Rust
Estrellas
7.2k
Forks
578
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

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 facebook/pyrefly

Todos los issues de facebook/pyrefly

Issues similares

Más issues de Rust

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.