Skip to content

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

@mgedmin

Description

@mgedmin

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 #13804 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-match-statementPython 3.10's match statement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions