-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
BinaryIO does not have peek method #3951
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
This is probably fine to add. The trouble with the IO protocols is that they are fairly ill-defined, in that there are lots of variations in exactly what methods different IO classes have, so it's tricky to say exactly which methods should be on each IO protocol. However, given that this method exists on the return type of No tests are necessary. |
I see. Thanks for the quick reply! |
@JelleZijlstra FYI, I opened a PR (sorry if you already got a notification from the mention there) |
I didn't see this discussion here, so I answered in the PR. But quickly: I don't think we should add |
I feel like the right solution is to make the
This would require making these classes inherit from I can look into this after #3371 and #4082 (which touch some of the same code) are merged. |
Agreed, that sounds like a much better solution! Let me know if I can be of help! |
Here's what
The code is at https://github.com/python/cpython/blob/master/Modules/_io/_iomodule.c#L231. |
This makes these classes usable if type annotations are given as "IO" or "TextIO". In the future, we'll then be able to move open() to return a concrete class instead (python#3951).
This makes these classes usable if type annotations are given as "IO" or "TextIO". In the future, we'll then be able to move open() to return a concrete class instead (python#3951).
Fixes python#3951. We use the values of the "mode" and "buffering" arguments to figure out the concrete type open() will return at runtime. (Compare the CPython code in https://github.com/python/cpython/blob/master/Modules/_io/_iomodule.c#L231.) I tested by writing a script that generated every mode/buffering combination, then running mypy with the open plugin disabled and comparing the results. This PR depends on python#4145.
Fixes python#3951. We use the values of the "mode" and "buffering" arguments to figure out the concrete type open() will return at runtime. (Compare the CPython code in https://github.com/python/cpython/blob/master/Modules/_io/_iomodule.c#L231.) I tested by writing a script that generated every mode/buffering combination, then running mypy with the open plugin disabled and comparing the results. This PR depends on python#4145.
This makes these classes usable if type annotations are given as "IO" or "TextIO". In the future, we'll then be able to move open() to return a concrete class instead (#3951).
* make io classes inherit from typing IO classes This makes these classes usable if type annotations are given as "IO" or "TextIO". In the future, we'll then be able to move open() to return a concrete class instead (#3951). * open: introduce concrete return types Fixes #3951. We use the values of the "mode" and "buffering" arguments to figure out the concrete type open() will return at runtime. (Compare the CPython code in https://github.com/python/cpython/blob/master/Modules/_io/_iomodule.c#L231.)
This makes these classes usable if type annotations are given as "IO" or "TextIO". In the future, we'll then be able to move open() to return a concrete class instead (python#3951).
* make io classes inherit from typing IO classes This makes these classes usable if type annotations are given as "IO" or "TextIO". In the future, we'll then be able to move open() to return a concrete class instead (python#3951). * open: introduce concrete return types Fixes python#3951. We use the values of the "mode" and "buffering" arguments to figure out the concrete type open() will return at runtime. (Compare the CPython code in https://github.com/python/cpython/blob/master/Modules/_io/_iomodule.c#L231.)
When opening a file with binary mode,
typing.BinaryIO
is inferred.This type should have the https://docs.python.org/3/library/io.html#io.BufferedReader.peek method.
It looks like this is known, judging by the TODO on this line in the
typing.pyi
stub:typeshed/stdlib/3/typing.pyi
Line 509 in bc90043
(There are also other
TODO
s there for BinaryIO - if those should be added, I could of course do those in the same change if that's not a problem.)A reproducible example:
Running mypy on this results in
error: "BinaryIO" has no attribute "peek"
.This issue has also been reported earlier in mypy.
I'd be glad to submit a PR for this. Is it as simple as adding
peek
toBinaryIO
intyping.pyi
? Or should there also be tests for these changes?The text was updated successfully, but these errors were encountered: