Skip to content

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

Merged
JukkaL merged 1 commit into
python:masterfrom
brianschubert:gh-18140-match-class-builtin-subtype
Nov 11, 2024
Merged

Fix type inference of positional parameter in class pattern involving builtin subtype#18141
JukkaL merged 1 commit into
python:masterfrom
brianschubert:gh-18140-match-class-builtin-subtype

Conversation

@brianschubert

Copy link
Copy Markdown
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'>

@github-actions

Copy link
Copy Markdown
Contributor

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

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

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
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