Skip to content

Pattern matching is not tested with @runtime_checkable protocols #110686

Closed
@sobolevn

Description

@sobolevn

Bug report

There are different simple and corner cases that we need to tests.
Why is that important? Because runtime_checkable Protocol has its own __instancecheck__ magic that must work correctly with match machinery.

>>> import typing, dataclasses
>>> @typing.runtime_checkable
... class P(typing.Protocol):
...    a: int
... 
>>> @dataclasses.dataclass
... class D:
...    a: int
... 
>>> match D(1):
...    case P() as p:
...        print(p)
... 
D(a=1)
>>> @typing.runtime_checkable
... class P(typing.Protocol):
...    a: int
...    b: int
... 
>>> @dataclasses.dataclass
... class D:
...     a: int
...     b: int
... 
>>> match D(1, 2):
...     case P(b=b1):
...         print(b1)
... 
2

I have a PR ready.

CC @AlexWaygood for runtime_checkable and @brandtbucher for match

Requires #110682 to test multiple cases with Protocol and __match_args__.

Linked PRs

Metadata

Metadata

Assignees

Labels

testsTests in the Lib/test dirtopic-typingtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions