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

Type narrowing on object attribute with match statement doesn't apply #14911

Open
AiroPi opened this issue Mar 16, 2023 · 1 comment
Open

Type narrowing on object attribute with match statement doesn't apply #14911

AiroPi opened this issue Mar 16, 2023 · 1 comment
Labels
feature topic-match-statement Python 3.10's match statement topic-type-narrowing Conditional type narrowing / binder

Comments

@AiroPi
Copy link

AiroPi commented Mar 16, 2023

The bug
It seems that doing a "type checking" with the object UI in pattern matching seems to not work properly.
Attribute match is not considered.

Code

class Test:
    def __init__(self, a: int | str):
        self.a: int | str = a

test = Test(5)

match test:
    case Test(a = int()):
        print(test.a + 3)
    case Test(a = str()):
        print(test.a + "3")

Expected behavior
By doing a match with Test(a = int()), python will check test is of type Test AND test.a is of type int.
But mypy "only assert" that test if of type Test. It keep the type int | str for test.a.

Reproduce :
On mypy-playground :
https://mypy-play.net/?mypy=latest&python=3.11&gist=6f05124abd1ef68032d59651a7d5c6c3

@AiroPi AiroPi added the bug mypy got something wrong label Mar 16, 2023
@erictraut
Copy link

For reference: microsoft/pyright#4775

@AlexWaygood AlexWaygood added feature topic-match-statement Python 3.10's match statement topic-type-narrowing Conditional type narrowing / binder and removed bug mypy got something wrong labels Mar 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature topic-match-statement Python 3.10's match statement topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

No branches or pull requests

3 participants