KEMBAR78
Fix type inference of positional parameter in class pattern involving builtin subtype by brianschubert · Pull Request #18141 · python/mypy · GitHub
Skip to content

Conversation

brianschubert
Copy link
Member

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