8000 Add typing.Reader and Writer protocols · python/cpython@b45fec0 · GitHub
[go: up one dir, main page]

Skip to content

Commit b45fec0

Browse files
committed
Add typing.Reader and Writer protocols
1 parent 208b0fb commit b45fec0

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

Doc/library/typing.rst

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2803,8 +2803,8 @@ with :func:`@runtime_checkable <runtime_checkable>`.
28032803
An ABC with one abstract method ``__round__``
28042804
that is covariant in its return type.
28052805

2806-
ABCs for working with IO
2807-
------------------------
2806+
ABCs and Protocols for working with I/O
2807+
---------------------------------------
28082808

28092809
.. class:: IO
28102810
TextIO
@@ -2815,6 +2815,28 @@ ABCs for working with IO
28152815
represent the types of I/O streams such as returned by
28162816
:func:`open`.
28172817

2818+
The following protocols offer a simpler alternative for common use cases. They
2819+
are especially useful for annotating function and method arguments and are
2820+
decorated with :func:`@runtime_checkable <runtime_checkable>`.
2821+
2822+
.. class:: Reader[T]
2823+
.. method:: read(size=...)
2824+
.. method:: readline(size=...)
2825+
.. method:: __iter__()
2826+
2827+
.. class:: Writer[T]
2828+
.. method:: write(o)
2829+
2830+
For example::
2831+
2832+
def read_it(reader: Reader[str]):
2833+
assert reader.read(11) == "--marker--\n"
2834+
for line in reader:
2835+
print(line)
2836+
2837+
def write_binary(writer: Writer[bytes]):
2838+
writer.write(b"Hello world!\n")
2839+
28182840
Functions and decorators
28192841
------------------------
28202842

Doc/whatsnew/3.14.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,14 @@ tkinter
607607
(Contributed by Zhikang Yan in :gh:`126899`.)
608608

609609

610+
typing
611+
------
612+
613+
* Add protocols :class:`typing.Reader` and :class:`typing.Writer` as a simpler
614+
alternatives to the pseudo-protocols :class:`typing.IO`,
615+
:class:`typing.TextIO`, and :class:`typing.BinaryIO`.
616+
617+
610618
unicodedata
611619
-----------
612620

Lib/typing.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
'AsyncContextManager',
9191

9292
# Structural checks, a.k.a. protocols.
93+
'Reader',
9394
'Reversible',
9495
'SupportsAbs',
9596
'SupportsBytes',
@@ -98,6 +99,7 @@
9899
'SupportsIndex',
99100
'SupportsInt',
100101
'SupportsRound',
102+
'Writer',
101103

102104
# Concrete collection types.
103105
'ChainMap',
@@ -2925,6 +2927,42 @@ def __round__(self, ndigits: int = 0) -> T:
29252927
pass
29262928

29272929

2930+
@runtime_checkable
2931+
class Reader[T](Iterable[T], Protocol):
2932+
"""Protocol for simple I/O reader instances.
2933+
2934+
This protocol only supports blocking I/O.
2935+
"""
2936+
2937+
__slots__ = ()
2938+
2939+
def read(self, size: int = ..., /) -> T:
2940+
"""Read data from the I/O stream and return it.
2941+
2942+
If "size" is specified, at most "size" items (bytes/characters) will be
2943+
read.
2944+
"""
2945+
def readline(self, size: int = ..., /) -> T:
2946+
"""Read a line of data from the I/O stream and return it.
2947+
2948+
If "size" is specified, at most "size" items (bytes/characters) will be
2949+
read.
2950+
"""
2951+
2952+
2953+
@runtime_checkable
2954+
class Writer[T](Protocol):
2955+
"""Protocol for simple I/O writer instances.
2956+
2957+
This protocol only supports blocking I/O.
2958+
"""
2959+
2960+
__slots__ = ()
2961+
2962+
def write(self, o: T, /) -> int:
2963+
"""Write data to the I/O stream and return number of items written."""
2964+
2965+
29282966
def _make_nmtuple(name, fields, annotate_func, module, defaults = ()):
29292967
nm_tpl = collections.namedtuple(name, fields,
29302968
defaults=defaults, module=module)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add protocols :class:`typing.Reader` and :class:`typing.Writer` as
2+
alternatives to :class:`typing.IO`, :class:`typing.TextIO`, and
3+
:class:`typing.BinaryIO`.

0 commit comments

Comments
 (0)
0