-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
"typing.BinaryIO" missing "readinto" method #133492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I think this should be fixed in |
The first change should be in typing, and then there is a followup (similar) change to typeshed. The actual change (to both) is pretty straightforward; I suspect it's more a question of should this method be included on BinaryIO. Considering that the |
Hi @DRayX Thanks for confirming |
Hi, @ZeroIntensity |
I'm not an expert here, but at a glance, changing the rules in an established protocol sounds like a no-go. I'd like to hear from @JelleZijlstra or @picnixz before giving someone a green light to work on this. |
As background on the
One of the weird things about these classes (IO, BinaryIO) is that they are not technically Protocols or ABCs. Instead, they are declared as regular classes; they have no subclasses at runtime but in typeshed we declare certain classes as subclasses anyway, so they mostly work in type annotations. This leads to unsound behavior; for example, type checkers will accept this:
But it also means that adding
This is already OK in typeshed; the runtime definitions of the methods in typing.py aren't used by type checkers. I wouldn't mind a PR updating the typing.py definitions to match those in typeshed better though. |
What practical problem does this solve? I would rather see type annotations fixed to use either concrete types or appropriate protocols than to encourage the use of these problematic pseudo-protocols. |
It's a bit contrived, but if you have a method that takes in the result of an |
I wonder whether it would make sense to have a protocol or type alias in |
I agree that a typeshed solution would be much more expedient. The comment you (@srittau) made on python/typeshed#2166 was that this should be resolved in I do agree though that long term moving typing.IO and it's subclasses to be protocols would make sense and give them more runtime utility. That seems like a much larger and even longer term change though. I think discussion of that could take place on the new python/typing#1994 issue. If there's no objections to adding |
Please note that the change to typeshed would follow the same version constraints as those of CPython. I.e. if it's added to Python 3.14, the new method will be behind a version guard in typeshed as well. |
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Bug description:
typeing.BinaryIO
is completely missing thereadinto
method. All binary mode return values ofopen
support thereadinto
method, and the lack of it on theBinaryIO
type makes type-hinting difficult when using this method. One could try to useio.RawIOBase | io.BufferedIOBase
but this looses the fact that the result ofopen
has aname
property. This has been brought up in python/typing#659 and python/typeshed#2166.Furthmore, the
write
method inBinaryIO
is declared to only be compatible withUnion[bytes, bytearray]
when it should be anything that satisfiescollections.abc.Buffer
.I propose that BinaryIO should look something more like:
CPython versions tested on:
CPython main branch
Operating systems tested on:
No response
The text was updated successfully, but these errors were encountered: