@@ -2737,6 +2737,28 @@ def close(self):
2737
2737
self .assertIsSubclass (B , Custom )
2738
2738
self .assertNotIsSubclass (A , Custom )
2739
2739
2740
+ @skipUnless (
2741
+ hasattr (collections .abc , "Buffer" ),
2742
+ "needs collections.abc.Buffer to exist"
2743
+ )
2744
+ @skip_if_py312b1
2745
+ def test_collections_abc_buffer_protocol_allowed (self ):
2746
+ @runtime_checkable
2747
+ class ReleasableBuffer (collections .abc .Buffer , Protocol ):
2748
+ def __release_buffer__ (self , mv : memoryview ) -> None : ...
2749
+
2750
+ class C : pass
2751
+ class D :
2752
+ def __buffer__ (self , flags : int ) -> memoryview :
2753
+ return memoryview (b'' )
2754
+ def __release_buffer__ (self , mv : memoryview ) -> None :
2755
+ pass
2756
+
2757
+ self .assertIsSubclass (D , ReleasableBuffer )
2758
+ self .assertIsInstance (D (), ReleasableBuffer )
2759
+ self .assertNotIsSubclass (C , ReleasableBuffer )
2760
+ self .assertNotIsInstance (C (), ReleasableBuffer )
2761
+
2740
2762
def test_builtin_protocol_allowlist (self ):
2741
2763
with self .assertRaises (TypeError ):
2742
2764
class CustomProtocol (TestCase , Protocol ):
@@ -2745,6 +2767,24 @@ class CustomProtocol(TestCase, Protocol):
2745
2767
class CustomContextManager (typing .ContextManager , Protocol ):
2746
2768
pass
2747
2769
2770
+ @skip_if_py312b1
2771
+ def test_typing_extensions_protocol_allowlist (self ):
2772
+ @runtime_checkable
2773
+ class ReleasableBuffer (Buffer , Protocol ):
2774
+ def __release_buffer__ (self , mv : memoryview ) -> None : ...
2775
+
2776
+ class C : pass
2777
+ class D :
2778
+ def __buffer__ (self , flags : int ) -> memoryview :
2779
+ return memoryview (b'' )
2780
+ def __release_buffer__ (self , mv : memoryview ) -> None :
2781
+ pass
2782
+
2783
+ self .assertIsSubclass (D , ReleasableBuffer )
2784
+ self .assertIsInstance (D (), ReleasableBuffer )
2785
+ self .assertNotIsSubclass (C , ReleasableBuffer )
2786
+ self .assertNotIsInstance (C (), ReleasableBuffer )
2787
+
2748
2788
def test_non_runtime_protocol_isinstance_check (self ):
2749
2789
class P (Protocol ):
2750
2790
x : int
0 commit comments