summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/test/test_typing.py16
-rw-r--r--Lib/typing.py2
-rw-r--r--Misc/NEWS.d/next/Library/2023-05-23-17-43-52.gh-issue-104797.NR7KzF.rst2
3 files changed, 19 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'],
}
diff --git a/Misc/NEWS.d/next/Library/2023-05-23-17-43-52.gh-issue-104797.NR7KzF.rst b/Misc/NEWS.d/next/Library/2023-05-23-17-43-52.gh-issue-104797.NR7KzF.rst
new file mode 100644
index 0000000..60c9a06
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-05-23-17-43-52.gh-issue-104797.NR7KzF.rst
@@ -0,0 +1,2 @@
+Allow :class:`typing.Protocol` classes to inherit from
+:class:`collections.abc.Buffer`. Patch by Jelle Zijlstra.