Skip to content

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

Merged
merged 3 commits into from
Oct 20, 2022

Conversation

ilinum
Copy link
Collaborator

@ilinum ilinum commented Oct 10, 2022

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.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

match a:
case _ if y is not None: # E: Name "y" may be undefined
pass
[builtins fixtures/tuple.pyi]
Copy link
Collaborator

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

Copy link
Collaborator Author

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.

Copy link
Collaborator

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.

Copy link
Collaborator Author

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.

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@JukkaL JukkaL merged commit 37044dd into python:master Oct 20, 2022
@ilinum ilinum deleted the partial/match branch November 22, 2022 21:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants