pyrefly icon indicating copy to clipboard operation
pyrefly copied to clipboard

Type of variable is forgotten in a loop where it is only accessed for read

Open kilroy42 opened this issue 7 months ago • 0 comments

Describe the Bug

This is a very strange bug, and I cannot really tell why this would happen. You need a very specific code to trigger it (I reduced the code from my codebase to a minimal example that does not look "sensible"):

numbers: list[int] = [0]

txt: str = ''           # must be declared outside the loop
for e in range(10):     # must be any loop (while True, for, ...)
    numbers[-1] += 1    # this triggers the bug
    txt += '.'.join([str(nr) for nr in numbers])     # must use "numbers" somehow, and assign to an outside variable

This will lead to:

test.py:5:5-16: Item assignment is not supported on `list[int] | Unknown`
  Expected `__setitem__` to be a callable, got BoundMethod[list[int], Overload[(Self@list, SupportsIndex, int) -> None, (Self@list, slice[Any, Any, Any], Iterable[int]) -> None]] | Unknown [not-callable]

The funny thing is, every line is important. You cannot remove or alter any of the lines.

Sandbox Link

https://pyrefly.org/sandbox/?code=HYVwtgRgpgTgzgLgAQBsCWcAuBtNxMC6SAvEtgAwEBQVmAHpsljCUgORtJfc9IDESMCCxJoSACZQAxigCGMKOKQB7EJjhpJSTAAsoqZcoAOVAGbKW+vEhizgAcygAKAIzkAlMh4ChIsXYBPA2MkJwB3HTQUfQAVGBAoABokcxhkgDpM9yoeUEhYOGwAWhciAGpSF29tSLhtGDR7R3ga-QgQexzuekwkCvZ0tnSAK2U8J2xmJ2AYdxSLJBmkazzoeAJ3IA

(Only applicable for extension issues) IDE Information

No response

kilroy42 avatar May 24 '25 15:05 kilroy42