python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.53k stars 2.83k forks source link

Positional subpattern of a built-in type subclass with __match_args__ causes type confusion #18140

Closed mgedmin closed 1 week ago

mgedmin commented 1 week ago

Given

class PrintableKey(bytes):
    __match_args__ = ('ch', )

    @property
    def ch(self) -> str:
        return self.decode('UTF-8')

now

match event:
    case PrintableKey(ch):
        reveal_type(ch)  # mypy thinks it's PrintableKey

versus

match event:
    case PrintableKey(ch=ch):
        reveal_type(ch)  # mypy correctly deduces it's str

Originally posted by @mgedmin in https://github.com/python/mypy/issues/13804#issuecomment-2466177933