-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Support match statement in partially defined check #13860
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
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
match a: | ||
case _ if y is not None: # E: Name "y" may be undefined | ||
pass | ||
[builtins fixtures/tuple.pyi] |
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.
This doesn't generate an error, though I think it should:
def f(x: int | str) -> int:
match x:
case int():
y = 1
return y # no error
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 exact example you provided generates the error for me. Can you double check how you ran this?
I've added this exact example into the test.
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.
Sorry, I was running the wrong code. The example works just fine. However, this related case seems to not work as expected:
def f(x: int | str) -> int:
match x:
case int():
y = 1
case str():
y = 2
return y # error: Name "y" may be undefined
Since the match statement covers all the union items, there shouldn't be an error. Similarly, this generates a false positive:
def f2(x: int | str) -> int:
if isinstance(x, int):
y = 1
elif isinstance(x, str):
y = 2
return y # error: Name "y" may be undefined
However, mypy doesn't complain about a missing return statement in these related examples, so mypy can already detect if all union items are covered:
def f3(x: int | str) -> int:
if isinstance(x, int):
return 1
elif isinstance(x, str):
return 2
def f4(x: int | str) -> int:
match x:
case int():
return 1
case str():
return 2
Since this also affects isinstance
checks, this may not be directly related to this PR, and fixed in a separate PR.
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.
Good find!
Agreed, separate PR is probably a better place to do this. I created #13926.
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
This adds support for match statements for the partially-defined variables check. This should completely cover all match features.
In addition, during testing, I found a bug in the generic branch tracking logic, which is also fixed here.
Because match is only supported in python 3.10, I had to put the tests in a separate file, which is a bit unfortunate; let me know if you have better ideas.