`unbound-name` does not consider `range()` with an integer correctly
Describe the Bug
for _ in range(3):
work = 1
print(work)
ERROR sandbox.py:4:7-11: `work` may be uninitialized [[unbound-name](https://pyrefly.org/en/docs/error-kinds/#unbound-name)]
Python: 3.12
If the argument passed to range can be determined to be above 0 work should not be considered unbound.
Sandbox Link
https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4uATgAQD6NE6NVGA5jABQDMAlIgB1mNEQHdqAaxoBeGgEYhQ4lSYAXTuKoTeIADQgArquhwS5RCADENAKrGoEVaRpgD6AMbHc6OIqwwwF2oAW1RVOnQDYOwYKk58REZ0VV4aAFoAPho4VSpBYREqGFUDKmYwARAAOSiYvJpgfABfSqE9EDIisChSQlVcYKgKawAFUi6e7IwcAhp3b0g2UrCIb0IhawBlGBgaAAtVVWI4RAB6U86AnsJqNlOYdFPMXHc4U-n0ReWvR6DaVAAbqhoKhsLA5gsIEtWD8aLhiD9TEpSKo9t40gDYnBVsxZJVuIQ5AAmVroEBNfSoTwQTEAMWgMAoaCweCIZHJQA
(Only applicable for extension issues) IDE Information
No response
This would be nice to catch at some point, we'll need some more complex flow-merging handling if we detect loops that are not skippable.
I think it's tricky to decide how far to go when determining that a loop definitely runs, but I imagine we can use a few rules for things like range or iterating on a literal iterable.
I understand. In pylint we also struggle with this, which is why we added possibly-used-before-assignment.
From an user's perspective issues like these do make it harder to migrate, as "false positives" don't help when making the case that pyrefly is better than the tooling we currently have.
For contributors:
The task is to add 2 special cases to the bindings of for-loops: calls to range() with a nonzero literal integer, and looping on a non-empty container like for x in [1, 2, 3]
@yangdanny97 shall i work on this. Will follow the approach you mentioned and create a PR.
I've got the fix, please assign me this issue then will create a PR
Hey @elden-sword I noticed this issue has been open for more than a weeks and thought I'd take a crack at it. I've opened PR #1851 with a fix that handles both cases mentioned by @yangdanny97 Feel free to review it, suggest changes, or take it over if you'd like, happy to collaborate! Just wanted to help move this forward. 🙂