-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Add support for jump statements in partially defined vars check #13632
Conversation
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome feature!
@@ -137,3 +144,158 @@ else: | |||
z = 2 | |||
|
|||
a = z + y # E: Name "y" may be undefined | |||
[case testReturn] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: will it work well together with rechability check? For example:
if int():
raise ValueError # or `return` or whatever
x = 1
else:
x = 2
a = x # ?
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example you provided will work. But here's an example where it doesn't work very well yet:
if False:
y = 1 # E: statement is unreachable
else:
x = 2
a = x # E: variable may be undefined
The plan is to check if a block is reachable, which I think should make the sample I posted return have only one error (the unreachable one).
This comment has been minimized.
This comment has been minimized.
2 similar comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Left a few minor comments.
I also noticed that this case still doesn't work as expected:
def f() -> None:
while True:
x = 1
if int():
break
print(x) # should not be an error
If the while condition is always true, the body is always executed at least once. It's okay to fix this in a follow-up PR if the fix is non-trivial.
x = 1 | ||
else: | ||
break | ||
y = x # No error -- x is always defined. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test accessing variables defined in the body of a loop that uses break outside the loop?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not quite sure what you mean. Are you referring to the example you gave in the other message?
def f() -> None:
while True:
x = 1
if int():
break
print(x) # should not be an error
Added support for this. One question is that I have a check for if I noticed that there's a function called |
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the updates!
Hmm, I don't remember why it behaves like this. I think that using |
Description
This builds on #13601 to add support for statements like
continue
,break
,return
,raise
in partially defined variables check. The simplest example is:Previously, mypy would generate a false positive on the last line of example. See test cases for more details.
Adding this support was relatively simple, given all the already existing code.
Things that aren't supported yet:
match
,with
, and detecting unreachable blocks.After this PR, when enabling this check on mypy itself, it generates 18 errors, all of them are potential bugs.
Test Plan
Tests added.