You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
unbound-name not reported after a for loop that contains break #5566
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.
deflast_value(values: list[int]) ->int:
forvalueinvalues:
last=valuereturnlast# UnboundLocalError when `values` is emptydeflast_before_zero(values: list[int]) ->int:
forvalueinvalues:
last=valueiflast==0:
breakreturnlast# UnboundLocalError when `values` is emptylast_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).
Activity
1-DARK commentedon Nov 7, 2026
#claim
github-actions commentedon Nov 7, 2026
@1-DARK you've claimed this issue — it's now assigned to you. Thanks for picking it up! 🎉