Allow Protocols to inherit from typing_extensions.Buffer or collections.abc.Buffer (#220)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 39bf2b5..13a9c3f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,9 @@
- Fix tests on Python 3.13, which removes support for creating
`TypedDict` classes through the keyword-argument syntax. Patch by
Jelle Zijlstra.
+- Allow `Protocol` classes to inherit from `typing_extensions.Buffer` or
+ `collections.abc.Buffer`. Patch by Alex Waygood (backporting
+ https://github.com/python/cpython/pull/104827, by Jelle Zijlstra).
# Release 4.6.3 (June 1, 2023)
diff --git a/src/test_typing_extensions.py b/src/test_typing_extensions.py
index a9fdcc0..5fa9c0c 100644
--- a/src/test_typing_extensions.py
+++ b/src/test_typing_extensions.py
@@ -2737,6 +2737,28 @@
self.assertIsSubclass(B, Custom)
self.assertNotIsSubclass(A, Custom)
+ @skipUnless(
+ hasattr(collections.abc, "Buffer"),
+ "needs collections.abc.Buffer to exist"
+ )
+ @skip_if_py312b1
+ def test_collections_abc_buffer_protocol_allowed(self):
+ @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):
@@ -2745,6 +2767,24 @@
class CustomContextManager(typing.ContextManager, Protocol):
pass
+ @skip_if_py312b1
+ def test_typing_extensions_protocol_allowlist(self):
+ @runtime_checkable
+ class ReleasableBuffer(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_non_runtime_protocol_isinstance_check(self):
class P(Protocol):
x: int
diff --git a/src/typing_extensions.py b/src/typing_extensions.py
index 1b92c39..5ac6dcf 100644
--- a/src/typing_extensions.py
+++ b/src/typing_extensions.py
@@ -453,9 +453,10 @@
_PROTO_ALLOWLIST = {
'collections.abc': [
'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
- 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
+ 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible', 'Buffer',
],
'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
+ 'typing_extensions': ['Buffer'],
}