possibly-undefined
rule should respect exhaustive checking
#14771
Labels
feature
topic-possibly-undefined
possibly-undefined error code
topic-reachability
Detecting unreachable code
Feature
If
assert_never
is used for exhaustive checking (per https://typing.readthedocs.io/en/latest/source/unreachable.html#assert-never-and-exhaustiveness-checking), then thepossibly-undefined
mypy rule should acknowledge that condition is impossible and so recognize when a variable must be defined.Pitch
Simple example (python 3.11)
With mypy 1.0.1, running
mypy --enable-error-code=possibly-undefined calculate.py
results in:Because we asserted that we can never enter the
else
case,result
should not be considered "possibly undefined" in thereturn
statement above.The
possibly-undefined
rule does behave properly when raising an exception, and should do the same withassert_never
. For instance, the following does not result in a mypy violation forpossibly-undefined
:The text was updated successfully, but these errors were encountered: