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

Fix type inference of positional parameter in class pattern involving builtin subtype #18141

Merged

Conversation

brianschubert
Copy link
Collaborator

Fixes #18140

Demo

from typing import reveal_type

class A(str):
    pass

class B(str):
    __match_args__ = ("b",)
    
    @property
    def b(self) -> int: return 1


match A("a"):
    case A(a):
        reveal_type(a)  # before: Revealed type is "__main__.A"
                        # after:  Revealed type is "__main__.A"
        print(type(a))  # output: <class '__main__.A'>

match B("b"):
    case B(b):
        reveal_type(b)  # before: Revealed type is "__main__.B"
                        # after:  Revealed type is "builtins.int"
        print(type(b))  # output: <class 'int'>

Copy link
Contributor

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

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Thanks for the fix!

@JukkaL JukkaL merged commit 54a2c6d into python:master Nov 11, 2024
19 checks passed
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.

Positional subpattern of a built-in type subclass with __match_args__ causes type confusion
2 participants