summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNikita Sobolev <mail@sobolevn.me>2023-10-12 02:20:20 (GMT)
committerGitHub <noreply@github.com>2023-10-12 02:20:20 (GMT)
commit5257ade0bc9b53471227d0e67999df7c8ad633a7 (patch)
tree7d9730bc78174e72da128cd5b5ea6b335fb5e6da
parent88ecb190f3717f7f0d663d004fc4b63c7e7bce77 (diff)
downloadcpython-5257ade0bc9b53471227d0e67999df7c8ad633a7.zip
cpython-5257ade0bc9b53471227d0e67999df7c8ad633a7.tar.gz
cpython-5257ade0bc9b53471227d0e67999df7c8ad633a7.tar.bz2
gh-110682: Ignore `__match_args__` from `__instancecheck__` in protocols (#110683)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
-rw-r--r--Lib/test/test_typing.py33
-rw-r--r--Lib/typing.py3
-rw-r--r--Misc/NEWS.d/next/Library/2023-10-11-11-00-11.gh-issue-110682.bXRFaX.rst4
3 files changed, 39 insertions, 1 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index c24cf3b..66812f3 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -3801,6 +3801,39 @@ class ProtocolTests(BaseTestCase):
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)
diff --git a/Lib/typing.py b/Lib/typing.py
index d1f3713..14845b3 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -1669,7 +1669,8 @@ _TYPING_INTERNALS = frozenset({
_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.
diff --git a/Misc/NEWS.d/next/Library/2023-10-11-11-00-11.gh-issue-110682.bXRFaX.rst b/Misc/NEWS.d/next/Library/2023-10-11-11-00-11.gh-issue-110682.bXRFaX.rst
new file mode 100644
index 0000000..b935d9a
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-10-11-11-00-11.gh-issue-110682.bXRFaX.rst
@@ -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.