Skip to content
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

Improve match statement union narrowing/inference #17600

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

bergkvist
Copy link

@bergkvist bergkvist commented Jul 27, 2024

Improve inference/narrowing support for union types in match statements.

Fixes #17549

Before:

var: tuple[int, int] | tuple[str, str]

# TODO: we can infer better here.
match var:
    case (42, a):
        reveal_type(a)  # N: Revealed type is "Union[builtins.int, builtins.str]"
    case ("yes", b):
        reveal_type(b)  # N: Revealed type is "Union[builtins.int, builtins.str]"

After:

var: tuple[int, int] | tuple[str, str]

match var:
    case (42, a):
        reveal_type(a)  # N: Revealed type is "builtins.int"
    case ("yes", b):
        reveal_type(b)  # N: Revealed type is "builtins.str"

Related:

This comment has been minimized.

Copy link
Contributor

@Hnasar Hnasar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!
Does your fix address any of the other match statement issues?
https://github.com/python/mypy/issues?q=sort%3Aupdated-desc+is%3Aopen+label%3Atopic-match-statement

mypy/checkpattern.py Outdated Show resolved Hide resolved

This comment has been minimized.

@bergkvist
Copy link
Author

Nice! Does your fix address any of the other match statement issues? https://github.com/python/mypy/issues?q=sort%3Aupdated-desc+is%3Aopen+label%3Atopic-match-statement

From reading through the issues, I believe it should at least solve these:

I notice there are a bunch of match exhaustiveness issues with narrowing of the type passed on to the next match branches, which this PR does not fix.

This comment has been minimized.

@hjwp
Copy link

hjwp commented Aug 21, 2024

maybe it would address this too? #16835

@bergkvist
Copy link
Author

maybe it would address this too? #16835

I don't think so, as I return the "current type" as the "rest type", which means the remainder/rest type (which is what gets passed on to the next match case) doesn't get narrowed properly yet. I have yet to figure out how to do this.

This comment has been minimized.

Copy link
Contributor

github-actions bot commented Sep 2, 2024

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

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.

Type is not being narrowed properly in match statement
3 participants