Conversation
BinaryIO.write was annotated as taking only bytes or bytearray, but the binary streams open() returns take any object with a buffer: BytesIO.write accepts a memoryview or an array.array just fine. typeshed spells this as ReadableBuffer on an IO[bytes] self-type overload, which a runtime stub cannot express, so the closest it can get is collections.abc.Buffer. writelines was off along two axes. IO.writelines said list[AnyStr] where the real method takes any iterable, and where typeshed says Iterable[AnyStr]. On the binary side it then stayed at bytes, although BytesIO.writelines swallows a list of memoryviews as readily as write swallows one memoryview; typeshed has a second self-type overload for exactly that, so BinaryIO now overrides writelines the way it already overrides write. Type checkers read typeshed rather than these annotations, so nothing about type checking changes; the runtime stubs just stop contradicting the stubs people check against.
The method is present on, or reachable from, every class typeshed marks as inheriting from BinaryIO. There are seven of them: _io.FileIO, BytesIO, BufferedReader, BufferedWriter and BufferedRandom pick it up from the raw and buffered base classes, http.client.HTTPResponse from io.BufferedIOBase, and codecs.StreamRecoder reaches it through __getattr__. StreamRecoder is not much of an exception there. typeshed already declares nine methods on it that the class itself does not have (close, fileno, flush, isatty, readable, truncate, seekable, tell and writable), with a comment saying they are delegated through __getattr__. Wrap a stream that has only read and write, and all nine raise AttributeError, exactly as readinto does. The return type is int rather than int | None for the same reason IO.read is AnyStr: a raw stream in non-blocking mode can return None from either, and these stubs already pass over that case.
v0ropaev
requested review from
AlexWaygood and
JelleZijlstra
as code owners
September 24, 2026 21:42
Documentation build overview
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #133492.
typing.BinaryIOhas noreadinto, though every binary streamopen()returns supports it, and itswrite/writelinesare narrower than what those streams accept.@JelleZijlstra set a condition for the
readintohalf:Seven classes in typeshed are declared that way.
FileIO,BytesIO,BufferedReader,BufferedWriterandBufferedRandomgetreadintofrom_RawIOBaseor_BufferedIOBase;http.client.HTTPResponsedeclares it itself. The seventh,codecs.StreamRecoder, does not define it — its__getattr__forwards every unknown attribute to the wrapped stream:so a recoder wrapping a binary stream has it and it works:
Worth noting that a class-level
hasattr(codecs.StreamRecoder, "readinto")saysFalseand is the wrong probe — it misses the forwarding, which only shows on an instance.The other half you were fine with outright:
So
BinaryIO.writeandBinaryIO.writelinesnow takecollections.abc.Bufferinstead ofbytes | bytearray, andIO.writelinestakes an iterable rather than a list. Binary streams accept all of that today; typeshed says the same through overloads a runtime stub cannot spell.Nothing here affects type checkers, which read typeshed rather than these definitions — this is about the runtime stubs matching what they claim to describe.
./python.exe -m test test_typingis 744 tests passing; revertingLib/typing.pywhile keeping the new test fails one. Two news entries, since the two halves stand on their own. Docs get aversionchanged:: nextonBinaryIO. main only.