-
-
Notifications
You must be signed in to change notification settings - Fork 32.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 1 commit
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
- Loading branch information
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,28 @@ 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] | ||||||
.. method:: read(size=...) | ||||||
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) 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. |
||||||
.. method:: readline(size=...) | ||||||
.. method:: __iter__() | ||||||
|
||||||
.. class:: Writer[T] | ||||||
.. method:: write(o) | ||||||
|
||||||
For example:: | ||||||
|
||||||
def read_it(reader: Reader[str]): | ||||||
assert reader.read(11) == "--marker--\n" | ||||||
for line in reader: | ||||||
print(line) | ||||||
|
||||||
def write_binary(writer: Writer[bytes]): | ||||||
writer.write(b"Hello world!\n") | ||||||
|
||||||
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.