ruff
6a2e6944 - [ty] Track literal iterable emptiness for reachability (#25222)

Commit
52 days ago
[ty] Track literal iterable emptiness for reachability (#25222) ## Summary This extends the static reachability analysis for synchronous `for` loops from #25220 to tuple, list, set, dictionary, string, and bytes literals whose emptiness is known syntactically. We record their static truthiness: a non-empty literal proves that the body executes at least once, while an empty literal marks the body unreachable and prevents its definitions from flowing past the loop. ```python for item in [1]: pass reveal_type(item) # Literal[1] value = 1 for _ in []: value = "unreachable" reveal_type(value) # Literal[1] ``` Starred elements and dictionary unpacking remain ambiguous when they are the only elements, while an explicit element still proves non-emptiness. Other iterables continue on the existing ambiguous path. Synchronous literals used in `async for` do not use this shortcut because they do not satisfy async iteration.
Author
Parents
Loading