Skip to content
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

__match_args__ + runtime_checkable protocol #110682

Closed
sobolevn opened this issue Oct 11, 2023 · 3 comments
Closed

__match_args__ + runtime_checkable protocol #110682

sobolevn opened this issue Oct 11, 2023 · 3 comments
Assignees
Labels
topic-typing type-bug An unexpected behavior, bug, or error

Comments

@sobolevn
Copy link
Member

sobolevn commented Oct 11, 2023

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

@sobolevn
Copy link
Member Author

Related #110686

@gvanrossum
Copy link
Member

I agree that __match_args__ ought to be disregarded by isinstance() in this case.

JelleZijlstra pushed a commit that referenced this issue Oct 12, 2023
…ols (#110683)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@AlexWaygood
Copy link
Member

Sounds like the consensus is that this was really a "missing feature" rather than a bug, so we won't be backporting this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-typing type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants