`unbound-name` not reported after a for loop that contains `break`
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
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
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
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de facebook/pyrefly
-
language-server
Dificultad 4/5 3-5 días Aptitud para principiantes 52/100
-
Dificultad 4/5 3-5 días Aptitud para principiantes 48/100
-
Dificultad 4/5 3-5 días Aptitud para principiantes 55/100
-
Attribute access after `if sys.platform != ...: return` is checked against the current platformAbierto
Dificultad 4/5 3-5 días Aptitud para principiantes 54/100
-
Dificultad 3/5 1-2 días Aptitud para principiantes 55/100
Todos los issues de facebook/pyrefly
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
aosama/astronomical#1973 ·
Los mantenedores suelen responder en 1 día
-
bug triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
Los mantenedores suelen responder en 6 días
-
complexity: high external-contributors test
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
Meet-hybrid/soroban-forge#584 ·
Los mantenedores suelen responder en 1 día
-
app bug pets
Dificultad 2/5 1-3 horas Aptitud para principiantes 66/100
Los mantenedores suelen responder en 1 día
-
chore(roles): record Elixir bare atom/keyword as StringLit excludes instead of coverage debtAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
dekobon/big-code-analysis#1859 ·
Los mantenedores suelen responder en 1 día