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

gh-110682: Ignore __match_args__ from __instancecheck__ in protocols #110683

Merged
merged 2 commits into from Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions Lib/test/test_typing.py
Expand Up @@ -3801,6 +3801,39 @@ class E:

self.assertIsInstance(E(), D)

def test_runtime_checkable_with_match_args(self):
@runtime_checkable
class P_regular(Protocol):
x: int
y: int

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

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

class WithMatch:
__match_args__ = ('x', 'y', 'z')
def __init__(self, x: int, y: int, z: int):
self.x = x
self.y = y
self.z = z

class Nope: ...

self.assertIsInstance(Regular(1, 2), P_regular)
self.assertIsInstance(Regular(1, 2), P_match)
self.assertIsInstance(WithMatch(1, 2, 3), P_regular)
self.assertIsInstance(WithMatch(1, 2, 3), P_match)
self.assertNotIsInstance(Nope(), P_regular)
self.assertNotIsInstance(Nope(), P_match)

def test_supports_int(self):
self.assertIsSubclass(int, typing.SupportsInt)
self.assertNotIsSubclass(str, typing.SupportsInt)
Expand Down
3 changes: 2 additions & 1 deletion Lib/typing.py
Expand Up @@ -1669,7 +1669,8 @@ class _TypingEllipsis:
_SPECIAL_NAMES = frozenset({
'__abstractmethods__', '__annotations__', '__dict__', '__doc__',
'__init__', '__module__', '__new__', '__slots__',
'__subclasshook__', '__weakref__', '__class_getitem__'
'__subclasshook__', '__weakref__', '__class_getitem__',
'__match_args__',
})

# These special attributes will be not collected as protocol members.
Expand Down
@@ -0,0 +1,4 @@
:func:`runtime-checkable protocols <typing.runtime_checkable>` used
to consider ``__match_args__`` a protocol member in
``__instancecheck__`` if it was present on the protocol. Now, this attribute is
ignored if it is present.