Skip to content

Commit 76003a8

Browse files
committed
Add tests
1 parent 3723370 commit 76003a8

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

Lib/test/test_io.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4916,6 +4916,24 @@ class PySignalsTest(SignalsTest):
49164916
test_reentrant_write_text = None
49174917

49184918

4919+
class ProtocolsTest(unittest.TestCase):
4920+
class MyReader:
4921+
def read(self, sz=-1):
4922+
return b""
4923+
4924+
class MyWriter:
4925+
def write(self, b: bytes):
4926+
pass
4927+
4928+
def test_reader_subclass(self):
4929+
self.assertIsSubclass(MyReader, io.Reader[bytes])
4930+
self.assertNotIsSubclass(str, io.Reader[bytes])
4931+
4932+
def test_writer_subclass(self):
4933+
self.assertIsSubclass(MyWriter, io.Writer[bytes])
4934+
self.assertNotIsSubclass(str, io.Writer[bytes])
4935+
4936+
49194937
def load_tests(loader, tests, pattern):
49204938
tests = (CIOTest, PyIOTest, APIMismatchTest,
49214939
CBufferedReaderTest, PyBufferedReaderTest,

Lib/test/test_typing.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4302,6 +4302,40 @@ def __release_buffer__(self, mv: memoryview) -> None:
43024302
self.assertNotIsSubclass(C, ReleasableBuffer)
43034303
self.assertNotIsInstance(C(), ReleasableBuffer)
43044304

4305+
def test_io_reader_protocol_allowed(self):
4306+
@runtime_checkable
4307+
class CustomReader(io.Reader[bytes], Protocol):
4308+
def close(self): ...
4309+
4310+
class A: pass
4311+
class B:
4312+
def read(self, sz=-1):
4313+
return b""
4314+
def close(self):
4315+
pass
4316+
4317+
self.assertIsSubclass(B, CustomReader)
4318+
self.assertIsInstance(B(), CustomReader)
4319+
self.assertNotIsSubclass(A, CustomReader)
4320+
self.assertNotIsInstance(A(), CustomReader)
4321+
4322+
def test_io_writer_protocol_allowed(self):
4323+
@runtime_checkable
4324+
class CustomWriter(io.Writer[bytes], Protocol):
4325+
def close(self): ...
4326+
4327+
class A: pass
4328+
class B:
4329+
def write(self, b):
4330+
pass
4331+
def close(self):
4332+
pass
4333+
4334+
self.assertIsSubclass(B, CustomWriter)
4335+
self.assertIsInstance(B(), CustomWriter)
4336+
self.assertNotIsSubclass(A, CustomWriter)
4337+
self.assertNotIsInstance(A(), CustomWriter)
4338+
43054339
def test_builtin_protocol_allowlist(self):
43064340
with self.assertRaises(TypeError):
43074341
class CustomProtocol(TestCase, Protocol):

0 commit comments

Comments
 (0)