Fix spurious possibly-undefined errors in for-else with break
When a for loop contains branches with break and an else block, variables declared inside those branches were incorrectly discarded from further analysis, leading Mypy to incorrectly report a variable as undefined after the loop or as used before declaration.
With this fix, when a for loop's else block is considered, variables declared in every branch of the for loop body that called break are now considered as defined within the body of the loop.
Fixes #14209 Fixes #19690
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅
@ilevkivskyi, I asked you for review since you reviewed the original change three years back.
It would be cool to get this landed, I just stumbled across a similar false positive in our codebase. Is there anything left to be done apart from actually merging this fix?
Is there anything left to be done apart from actually merging this fix?
Yes: https://github.com/python/mypy/pull/19696/files#r2300505910