Mypy should detect obvious UnboundLocalErrors
(e.g. declarations missing from an else:
branch)
#13696
Labels
UnboundLocalErrors
(e.g. declarations missing from an else:
branch)
#13696
Feature
If a variable is only declared in an
if
branch, but used afterwards, mypy should throw an error.Pitch
Currently, the following snippet passes
mypy --strict
(v0.971
/py3.9):Additionally, it reveals
builtins.int
for a clearly underspecified type.This is pretty surprising, and in fact I did not catch an obvious bug because I mistakenly remembered that mypy had warned me about such issues in the past – but also because I assumed imypy could not possibly infer
int
in such a case: If I had a conflicting declaration in theelse
block, mypy would have already complained! So to some extent one might say this violates the principle of least surprise.Relates to #686 and #6233 (specifically this comment).
(I hope this is not a duplicate)
The text was updated successfully, but these errors were encountered: