-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-127647: Add typing.Reader and Writer protocols #127648
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
Changes from 6 commits
b45fec0
1525e05
7867ec1
6a22a02
5d632a3
4d50c2e
1e1ea41
56a38a0
f2c331b
6764b6a
022acaa
b86073d
65eb040
2b9159d
1f42b21
0325f5a
632511a
3b384f9
5bdb4cc
35dcaf4
5584a57
af81301
5a8b915
b1593fa
577b893
cedfa42
a0b9e47
53a2250
03aa3a2
ca72c19
3b5975e
96080fe
3723370
76003a8
43e23f0
c644770
bfab2fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2803,8 +2803,8 @@ with :func:`@runtime_checkable <runtime_checkable>`. | |||||
| An ABC with one abstract method ``__round__`` | ||||||
| that is covariant in its return type. | ||||||
|
|
||||||
| ABCs for working with IO | ||||||
| ------------------------ | ||||||
| ABCs and Protocols for working with I/O | ||||||
| --------------------------------------- | ||||||
|
|
||||||
| .. class:: IO | ||||||
| TextIO | ||||||
|
|
@@ -2815,6 +2815,56 @@ ABCs for working with IO | |||||
| represent the types of I/O streams such as returned by | ||||||
| :func:`open`. | ||||||
|
|
||||||
| The following protocols offer a simpler alternative for common use cases. They | ||||||
| are especially useful for annotating function and method arguments and are | ||||||
| decorated with :func:`@runtime_checkable <runtime_checkable>`. | ||||||
|
|
||||||
| .. class:: Reader[T] | ||||||
|
|
||||||
| Protocol for reading from a file or other input stream. | ||||||
|
|
||||||
| .. method:: read(size=...) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
(Same for other methods)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was unsure of how much of the signature to document. For example, would it also make sense to add argument and/or return types? I've added the slashes for now. |
||||||
|
|
||||||
| Read data from the input stream and return it. If ``size`` is | ||||||
|
srittau marked this conversation as resolved.
Outdated
|
||||||
| specified, at most ``size`` items (bytes/characters) will be read. | ||||||
|
|
||||||
| .. method:: readline(size=...) | ||||||
|
|
||||||
| Read a line of data from the input stream and return it. If ``size`` is | ||||||
| specified, at most ``size`` items (bytes/characters) will be read. | ||||||
|
|
||||||
| .. method:: __iter__() | ||||||
|
|
||||||
| Read a line of data from the input stream and return it. | ||||||
|
srittau marked this conversation as resolved.
Outdated
|
||||||
|
|
||||||
| For example:: | ||||||
|
|
||||||
| def read_it(reader: Reader[str]): | ||||||
| assert reader.read(11) == "--marker--\n" | ||||||
| for line in reader: | ||||||
| print(line) | ||||||
|
|
||||||
| .. class:: Writer[T] | ||||||
|
|
||||||
| Protocol for writing to a file or other output stream. | ||||||
|
|
||||||
| .. method:: write(data) | ||||||
|
|
||||||
| Write data to the output stream and return number of items | ||||||
| (bytes/characters) written. | ||||||
|
|
||||||
| For example:: | ||||||
|
|
||||||
| def write_binary(writer: Writer[bytes]): | ||||||
| writer.write(b"Hello world!\n") | ||||||
|
|
||||||
| Also consider using :class:`collections.abc.Iterable` for iterating over | ||||||
| the lines of an input stream:: | ||||||
|
|
||||||
| def read_config(stream: Iterable[str]): | ||||||
| for line in stream: | ||||||
| ... | ||||||
|
srittau marked this conversation as resolved.
Outdated
|
||||||
|
|
||||||
| Functions and decorators | ||||||
| ------------------------ | ||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Add protocols :class:`typing.Reader` and :class:`typing.Writer` as | ||
| alternatives to :class:`typing.IO`, :class:`typing.TextIO`, and | ||
| :class:`typing.BinaryIO`. |
Uh oh!
There was an error while loading. Please reload this page.