Skip to content

Pattern matching is not tested with @runtime_checkable protocols #110686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sobolevn opened this issue Oct 11, 2023 · 1 comment
Closed

Pattern matching is not tested with @runtime_checkable protocols #110686

sobolevn opened this issue Oct 11, 2023 · 1 comment
Assignees
Labels
tests Tests in the Lib/test dir topic-typing type-bug An unexpected behavior, bug, or error

Comments

@sobolevn
Copy link
Member

sobolevn commented Oct 11, 2023

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

@hugovk
Copy link
Member

hugovk commented Dec 11, 2023

@sobolevn Ready to close this issue?

aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Tests in the Lib/test dir topic-typing type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants