Skip to content

Pattern matching is not tested with @runtime_checkable protocols #110686

Closed

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

Activity

  1. added
    type-bug
    tests
    on Oct 11, 2023
  2. self-assigned this
    on Oct 11, 2023
  3. added a commit that references this issue on Oct 11, 2023
  4. added a commit that references this issue on Dec 10, 2023
  5. hugovk commented on Dec 11, 2023

    @hugovk
    Member

    @sobolevn Ready to close this issue?

  6. added a commit that references this issue on Feb 11, 2024
  7. added a commit that references this issue on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

teststopic-typingtype-bug

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions