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
gh-100226: Clarify StreamReader.read behavior #101807
base: main
Are you sure you want to change the base?
Conversation
This clarifies the `StreamReader.read` behavior in the public documentation; mostly copying from the more precise docstring of the function. While we're at it, also fix some minor grammar in the docstring itself. Addresses python#100226.
|
Most changes to Python require a NEWS entry. Please add it using the blurb_it web app or the blurb command-line tool. |
|
This does not seem to require a NEWS entry to me, but I'm happy to add one if I'm mistaken. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Standard reminder: You can directly apply all the suggestions you want in one go with Files changed -> Add to batch -> Commit
Sorry for the delay reviewing this. This is certainly an improvement—I'd suggest a number of further clarifications and revisions to fix things that still initially confused me.
| If *n* is not provided, or set to ``-1``, | ||
| read until EOF. After EOF is received, return all read bytes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| If *n* is not provided, or set to ``-1``, | |
| read until EOF. After EOF is received, return all read bytes. | |
| If *n* is not provided or set to ``-1``, | |
| read until EOF (``EOT``, ``b"\x04"``/:kbd:`Ctrl-D`) is received, | |
| then return all read :class:`bytes`. |
- Clarify that in this context (at least, I'm assuming this is the case if this follows the Unix
readsemantics like Guido indicated), EOF is specifically theEOTcharacter code, rather thanSUB(\x1A/Ctrl-Z, the Windows EOF), some other byte or some other out of band signalling. - Join the sentences accordingly to avoid repetition the same
- Be more explicit and cross-reference the returned data type
- Elide unnecessary internal comma
| If EOF was received and the internal buffer is empty, | ||
| return an empty ``bytes`` object. | ||
|
|
||
| If *n* is zero, return empty ``bytes`` object immediately. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| If *n* is zero, return empty ``bytes`` object immediately. | |
| If *n* is ``0``, return an empty ``bytes`` object immediately. |
- Fix grammar
- Use the appropriate literal
| If *n* is positive, this function tries to read *n* bytes, and may return | ||
| less or equal bytes than requested, but at least one byte. If EOF was | ||
| received before any byte is read, this function returns an empty | ||
| ``bytes`` object. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| If *n* is positive, this function tries to read *n* bytes, and may return | |
| less or equal bytes than requested, but at least one byte. If EOF was | |
| received before any byte is read, this function returns an empty | |
| ``bytes`` object. | |
| If *n* is positive, read at most *n* bytes. | |
| May return less than *n* bytes, if EOF is reached first. | |
| If EOF is received before any byte is read, | |
| return an empty ``bytes`` object. |
- Specify the actual reason for less than n bytes being returned—EOF is reached first (assuming I'm interpreting this correctly, and it parallels the
-1case). - Avoid apparent contradictory statement that at least one byte will be returned, yet can also return empty bytes if EOF is received first
- Split first sentence into two, for clarity and readability
- Elide redundant (and imprecise) "this function" verbiage, and be consistent with the phrasing used elsewhere here
| @@ -206,12 +206,20 @@ StreamReader | |||
|
|
|||
| .. coroutinemethod:: read(n=-1) | |||
|
|
|||
| Read up to *n* bytes. If *n* is not provided, or set to ``-1``, | |||
| read until EOF and return all read bytes. | |||
| Read up to *n* bytes. | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Read up to *n* bytes. | |
| Read up to *n* bytes from the stream. |
For explicitness, and to match the docstring version
| If n is positive, this function try to read `n` bytes, and may return | ||
| If n is positive, this function tries to read `n` bytes, and may return | ||
| less or equal bytes than requested, but at least one byte. If EOF was | ||
| received before any byte is read, this function returns empty byte | ||
| received before any byte is read, this function returns an empty bytes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest porting the refined version from the above here as well.
Yes; standard practice is to backport any docs-only fixes that affect bugfix-supported branches, as it fixes a docs defect and ensures that other changes can be backported cleanly too. I've added the appropriate tags to line up the backports when this is merged.
No need, you're quite right—it's a docs-only fix. |
|
No time to review today, but once CAM approves I'll skim and merge. |


This clarifies the
StreamReader.readbehavior in the public documentation; mostly copying from the more precise docstring of the function.While we're at it, also fix some minor grammar in the docstring itself.
Addresses #100226.
Should this be backported so that the documentation for older releases is also updated?
asyncio.StreamReader.readdoes what exactly? #100226