8000 `__match_args__` + `runtime_checkable` protocol · Issue #110682 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
__match_args__ + runtime_checkable protocol #110682
Closed
@sobolevn

Description

@sobolevn

Bug report

Since __match_args__ is not mentioned in _SPECIAL_NAMES right now these two examples have different results:

@runtime_checkable
class P(Protocol):
    x: int
    y: int

class A:
    def __init__(self, x: int, y: int):
        self.x = x
        self.y = y

assert isinstance(A(1, 2), P) is True

And:

@runtime_checkable
class P(Protocol):
    __match_args__ = ('x', 'y')
    x: int
    y: int

class A:
    def __init__(self, x: int, y: int):
        self.x = x
        self.y = y

assert isinstance(A(1, 2), P) is False

Why I think that __match_args__ is a special attribute and should not be checked in isinstance?

  1. It might be useed for protocol itself in patma (new issue is on its way about it):
match A(1, 2):
    case P(x, y):
        print(x, y)

But, this does not work right now if A does not have __match_args__. Which is not really required for this case.

  1. Several similar attributes like __slots__ and __weakref__ and __annotations__ are ignored already

Do others agree?
CC @AlexWaygood for @runtime_checkable protocols

I will send a PR with my proposed solution and tests :)

Linked PRs

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0