-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[red-knot] Control flow modelling error in try
... else
#14908
Comments
I spent a while thinking that this had something to do with the fact that we don't yet model the fact that simple statements like from typing import Literal
def may_raise() -> Literal[2]:
return 2
x = 1
try:
may_raise()
x = may_raise()
except:
pass
else:
x = 3
reveal_type(x) # revealed: Literal[1, 2, 3] Like your example, we reveal
This indicates that to get the desired result here, we need to have some special handling for assignments when the assignment is the final statement in a Concern about
|
In fact, even for assignments that are created via statements rather than assignment expressions, simply looking at whether the assignment occurs in "the last statement" of the def may_raise(): ...
x = 1
try:
may_raise()
if True:
x = 2
may_raise()
except:
pass
else:
x = 3 We would have to recursively examine whether the assignment takes place in the last statement of the last statement of the last statement of [etc. for however many substatements there are] the |
#14015 mentions a problem with
finally
suites, but it looks like we also don't modeltry
…else
control flow correctly in all cases.We currently infer
Literal[1, 2, 3]
here, but it should beLiteral[1, 3]
?The text was updated successfully, but these errors were encountered: