Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
b45fec01525e057867ec16a22a025d632a34d50c2e1e1ea4156a38a0f2c331b6764b6a022acaab86073d65eb0402b9159d1f42b210325f5a632511a3b384f95bdb4cc35dcaf45584a57af813015a8b915b1593fa577b893cedfa42a0b9e4753a225003aa3a2ca72c193b5975e96080fe372337076003a843e23f0c644770bfab2fdFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -46,12 +46,14 @@ | ||||||
| "BufferedReader", "BufferedWriter", "BufferedRWPair", | ||||||
| "BufferedRandom", "TextIOBase", "TextIOWrapper", | ||||||
| "UnsupportedOperation", "SEEK_SET", "SEEK_CUR", "SEEK_END", | ||||||
| "DEFAULT_BUFFER_SIZE", "text_encoding", "IncrementalNewlineDecoder"] | ||||||
| "DEFAULT_BUFFER_SIZE", "text_encoding", "IncrementalNewlineDecoder", | ||||||
| "Reader", "Writer"] | ||||||
| import _io | ||||||
| import abc | ||||||
| from _collections_abc import _check_methods | ||||||
| from _io import (DEFAULT_BUFFER_SIZE, BlockingIOError, UnsupportedOperation, | ||||||
| open, open_code, FileIO, BytesIO, StringIO, BufferedReader, | ||||||
| BufferedWriter, BufferedRWPair, BufferedRandom, | ||||||
| @@ -97,3 +99,55 @@ class TextIOBase(_io._TextIOBase, IOBase): | ||||||
| pass | ||||||
| else: | ||||||
| RawIOBase.register(_WindowsConsoleIO) | ||||||
| # | ||||||
| # Static Typing Support | ||||||
| # | ||||||
| GenericAlias = type(list[int]) | ||||||
| class Reader(metaclass=abc.ABCMeta): | ||||||
| """Protocol for simple I/O reader instances. | ||||||
| This protocol only supports blocking I/O. | ||||||
| """ | ||||||
| __slots__ = () | ||||||
| @abc.abstractmethod | ||||||
| def read(self, 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. Sorry if this has been discussed before, but I'm unsure on the runtime use of Almost every other Would it be better to have Suggested change
A ContributorAuthor
| ||||||
| """Read data from the input stream and return it. | ||||||
| If *size* is specified, at most *size* items (bytes/characters) will be | ||||||
| read. | ||||||
| """ | ||||||
| @classmethod | ||||||
| def __subclasshook__(cls, C): | ||||||
| if cls is Reader: | ||||||
| return _check_methods(C, "read") | ||||||
| return NotImplemented | ||||||
| __class_getitem__ = classmethod(GenericAlias) | ||||||
| class Writer(metaclass=abc.ABCMeta): | ||||||
| """Protocol for simple I/O writer instances. | ||||||
| This protocol only supports blocking I/O. | ||||||
| """ | ||||||
| __slots__ = () | ||||||
| @abc.abstractmethod | ||||||
| def write(self, data, /): | ||||||
| """Write *data* to the output stream and return the number of items written.""" | ||||||
| @classmethod | ||||||
| def __subclasshook__(cls, C): | ||||||
| if cls is Writer: | ||||||
| return _check_methods(C, "write") | ||||||
| return NotImplemented | ||||||
| __class_getitem__ = classmethod(GenericAlias) | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Add protocols :class:`io.Reader` and :class:`io.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.
Uh oh!
There was an error while loading. Please reload this page.