summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2023-05-24 08:24:53 (GMT)
committerGitHub <noreply@github.com>2023-05-24 08:24:53 (GMT)
commitc0ab7d401c736c37bf4462eef7c7d69fef8fab93 (patch)
tree99259a8ab3c26c47995995fcb82ddbdba1948f3d /Lib
parent4b56e56c495de58425ae3db5f4d8183127ee990b (diff)
downloadcpython-c0ab7d401c736c37bf4462eef7c7d69fef8fab93.zip
cpython-c0ab7d401c736c37bf4462eef7c7d69fef8fab93.tar.gz
cpython-c0ab7d401c736c37bf4462eef7c7d69fef8fab93.tar.bz2
gh-104797: Allow Protocols to inherit from collections.abc.Buffer (#104827)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_typing.py16
-rw-r--r--Lib/typing.py2
2 files changed, 17 insertions, 1 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index b4a5a68..098933b 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -3546,6 +3546,22 @@ class ProtocolTests(BaseTestCase):
self.assertIsSubclass(B, Custom)
self.assertNotIsSubclass(A, Custom)
+ @runtime_checkable
+ class ReleasableBuffer(collections.abc.Buffer, Protocol):
+ def __release_buffer__(self, mv: memoryview) -> None: ...
+
+ class C: pass
+ class D:
+ def __buffer__(self, flags: int) -> memoryview:
+ return memoryview(b'')
+ def __release_buffer__(self, mv: memoryview) -> None:
+ pass
+
+ self.assertIsSubclass(D, ReleasableBuffer)
+ self.assertIsInstance(D(), ReleasableBuffer)
+ self.assertNotIsSubclass(C, ReleasableBuffer)
+ self.assertNotIsInstance(C(), ReleasableBuffer)
+
def test_builtin_protocol_allowlist(self):
with self.assertRaises(TypeError):
class CustomProtocol(TestCase, Protocol):
diff --git a/Lib/typing.py b/Lib/typing.py
index 95dbc0b..b32ff0c 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -1740,7 +1740,7 @@ def _allow_reckless_class_checks(depth=3):
_PROTO_ALLOWLIST = {
'collections.abc': [
'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
- 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
+ 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible', 'Buffer',
],
'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
}