Repository navigation
io.BufferedReader: typeshed vs Python documentation
#11792
Replies: 2 comments
|
You can keep subclassing reader = io.BufferedReader(raw) # raw is the stream object
raw.readinto(b) # b is the destination bufferThe
Importantly, If you're overriding For an annotation you can import at runtime, use The PEP 688 comment refers to a separate limitation: the buffer type doesn't currently distinguish read-only from writable buffers. It doesn't mean the intended behavior of If that still leaves a typing error, a small example of the subclass together with the exact diagnostic would help distinguish an override incompatibility from a possible stub or type-checker issue. |
Yes, I was talking about both, may have not checked enough there was no risk of confusion for readers.
:-) Was to investigate that, after I saw this
Indeed, it shortly got my attention while searching in the typeshed, but I finally overlooked it (sigh). Will see with this too and see what looks nicer. Thanks for the nice and readable wordings |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
For some things, I needed to create an
io.BufferedReaderand needed to have its first argument to be of a custom class. I first started with the Python API documentation: https://docs.python.org/3/library/io.html#io.BufferedReader . About the argument, namedraw, it says:I’m always at doubts when reading documentation carefully (or trying to), so, here, I first wonder if I had correctly understood this as meaning
rawis aRawIOBase, as I initially understood it. At least, it make sense … raw … RawIOBase …Later I had to solve a typing issue when defining the overriding
readinto. ForRawIOBase, it expects aWriteableBufferargument. I had difficulties with it, because there is no such class defined anyway, it exist only as an informal description in the documentation and as a non-informative definition in the TypeShed. I did the best I could, when suddenly, on the opportunity of later type error message elsewhere, I learned it should instead be amemoryview(for the argument toreadinto). So I looked at the TypeShed and fount this: https://github.com/python/typeshed/blob/a65dab44b7c0f6b21169d8f28f33e49fa20a91ae/stdlib/_io.pyi#L155 . It says the argument is a_BufferedReaderStreamTwhich is a type variable for a_BufferedReaderStreamand itsreadintoexpect amemoryview, according to this line: https://github.com/python/typeshed/blob/a65dab44b7c0f6b21169d8f28f33e49fa20a91ae/stdlib/_io.pyi#L130 . At least, it was a confirmation of what the error message said, so it seems to be a good track to follow.I’m feeling a bit unsure due to this difference, while there is better chance to have good typing, with following the TypeShed rather than the a bit informal documentation. Still, is it safe? Because a
memoryviewis not the same as aRawIOBase…Also, trying to learn about
WriteableBufferwhich does not exist in the documentation, I found this, just a few line above its definition: https://github.com/python/typeshed/blob/a65dab44b7c0f6b21169d8f28f33e49fa20a91ae/stdlib/_typeshed/__init__.pyi#L320Is this the reason? Uncertainty about what the definition should be in some aspect in the future?
I’m too unsure to open an issue on the typeshed repository which does not provide the discussion feature, neither the pythondotorg repository.
Since open discussions are allowed here, I took this opportunity. If there is a better place for this post, please, let me know so that I repost it anywhere else.
Now I will try derive the class from another one, but unfortunately, there is no
BufferedReaderStreamclass in the API, so may try to cheat for a necessity (if possible). That’s just a spontaneous small talk, because it is not the main topic.All reactions