Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3027,6 +3027,10 @@ ABCs and Protocols for working with I/O
:func:`open`. Please note that these classes are not protocols, and
their interface is fairly broad.

.. versionchanged:: next
``BinaryIO`` now declares a ``readinto()`` method, matching the binary
streams that :func:`open` returns.

The protocols :class:`io.Reader` and :class:`io.Writer` offer a simpler
alternative for argument types, when only the ``read()`` or ``write()``
methods are accessed, respectively::
Expand Down
23 changes: 23 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9660,6 +9660,29 @@ def stuff(a: BinaryIO) -> bytes:
a = stuff.__annotations__['a']
self.assertEqual(a.__parameters__, ())

def test_write_annotations(self):
# These are evaluated lazily, so a wrong name in any of them would
# stay invisible until something introspects the stub. Compare the
# whole mapping so that a dropped or stray annotation shows up too.
self.assertEqual(
IO.writelines.__annotations__,
{'lines': collections.abc.Iterable[AnyStr], 'return': None})
self.assertEqual(
BinaryIO.write.__annotations__,
{'s': collections.abc.Buffer, 'return': int})
self.assertEqual(
BinaryIO.writelines.__annotations__,
{'lines': collections.abc.Iterable[collections.abc.Buffer],
'return': None})

def test_binaryio_readinto(self):
# Every class typeshed marks as a BinaryIO has readinto(), so the
# runtime stub declares it as well.
self.assertTrue(BinaryIO.readinto.__isabstractmethod__)
self.assertEqual(
BinaryIO.readinto.__annotations__,
{'buffer': collections.abc.Buffer, 'return': int})


class RETests(BaseTestCase):
# Much of this is really testing _TypeAlias.
Expand Down
14 changes: 12 additions & 2 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3636,7 +3636,7 @@ def write(self, s: AnyStr, /) -> int:
pass

@abstractmethod
def writelines(self, lines: list[AnyStr], /) -> None:
def writelines(self, lines: collections.abc.Iterable[AnyStr], /) -> None:
pass

@abstractmethod
Expand All @@ -3654,7 +3654,17 @@ class BinaryIO(IO[bytes]):
__slots__ = ()

@abstractmethod
def write(self, s: bytes | bytearray, /) -> int:
def readinto(self, buffer: collections.abc.Buffer, /) -> int:
pass

@abstractmethod
def write(self, s: collections.abc.Buffer, /) -> int:
pass

@abstractmethod
def writelines(
self, lines: collections.abc.Iterable[collections.abc.Buffer], /
) -> None:
pass

@abstractmethod
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
:class:`typing.BinaryIO` now annotates ``write()`` and ``writelines()`` in
terms of :class:`collections.abc.Buffer` rather than :class:`bytes`, and
``writelines()`` on :class:`typing.IO` takes any iterable rather than a list.
Binary streams accept all of this already, and typeshed says the same through
overloads that a runtime stub has no way to spell.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:class:`typing.BinaryIO` now declares a ``readinto()`` method. Every class
that typeshed marks as inheriting from ``BinaryIO`` provides it, in
:class:`codecs.StreamRecoder`'s case through ``__getattr__``, so the runtime
stub was missing a method the type describes.
Loading