Skip to content

Commit 42251fb

Browse files
sobolevnAlexWaygood
authored andcommitted
pythongh-110682: Ignore __match_args__ from __instancecheck__ in protocols (python#110683)
Co-authored-by: Alex Waygood <[email protected]>
1 parent 022ef91 commit 42251fb

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

Lib/test/test_typing.py

+33
Original file line numberDiff line numberDiff line change
@@ -3801,6 +3801,39 @@ class E:
38013801

38023802
self.assertIsInstance(E(), D)
38033803

3804+
def test_runtime_checkable_with_match_args(self):
3805+
@runtime_checkable
3806+
class P_regular(Protocol):
3807+
x: int
3808+
y: int
3809+
3810+
@runtime_checkable
3811+
class P_match(Protocol):
3812+
__match_args__ = ('x', 'y')
3813+
x: int
3814+
y: int
3815+
3816+
class Regular:
3817+
def __init__(self, x: int, y: int):
3818+
self.x = x
3819+
self.y = y
3820+
3821+
class WithMatch:
3822+
__match_args__ = ('x', 'y', 'z')
3823+
def __init__(self, x: int, y: int, z: int):
3824+
self.x = x
3825+
self.y = y
3826+
self.z = z
3827+
3828+
class Nope: ...
3829+
3830+
self.assertIsInstance(Regular(1, 2), P_regular)
3831+
self.assertIsInstance(Regular(1, 2), P_match)
3832+
self.assertIsInstance(WithMatch(1, 2, 3), P_regular)
3833+
self.assertIsInstance(WithMatch(1, 2, 3), P_match)
3834+
self.assertNotIsInstance(Nope(), P_regular)
3835+
self.assertNotIsInstance(Nope(), P_match)
3836+
38043837
def test_supports_int(self):
38053838
self.assertIsSubclass(int, typing.SupportsInt)
38063839
self.assertNotIsSubclass(str, typing.SupportsInt)

Lib/typing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1669,7 +1669,8 @@ class _TypingEllipsis:
16691669
_SPECIAL_NAMES = frozenset({
16701670
'__abstractmethods__', '__annotations__', '__dict__', '__doc__',
16711671
'__init__', '__module__', '__new__', '__slots__',
1672-
'__subclasshook__', '__weakref__', '__class_getitem__'
1672+
'__subclasshook__', '__weakref__', '__class_getitem__',
1673+
'__match_args__',
16731674
})
16741675

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

0 commit comments

Comments
 (0)