mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
bpo-31709: Drop support for asynchronous __aiter__. (#3903)
This commit is contained in:
parent
86566702f3
commit
faa135acbf
9 changed files with 47 additions and 300 deletions
|
@ -2520,9 +2520,8 @@ generators, coroutines do not directly support iteration.
|
|||
Asynchronous Iterators
|
||||
----------------------
|
||||
|
||||
An *asynchronous iterable* is able to call asynchronous code in its
|
||||
``__aiter__`` implementation, and an *asynchronous iterator* can call
|
||||
asynchronous code in its ``__anext__`` method.
|
||||
An *asynchronous iterator* can call asynchronous code in
|
||||
its ``__anext__`` method.
|
||||
|
||||
Asynchronous iterators can be used in an :keyword:`async for` statement.
|
||||
|
||||
|
@ -2552,48 +2551,14 @@ An example of an asynchronous iterable object::
|
|||
|
||||
.. versionadded:: 3.5
|
||||
|
||||
.. note::
|
||||
.. versionchanged:: 3.7
|
||||
Prior to Python 3.7, ``__aiter__`` could return an *awaitable*
|
||||
that would resolve to an
|
||||
:term:`asynchronous iterator <asynchronous iterator>`.
|
||||
|
||||
.. versionchanged:: 3.5.2
|
||||
Starting with CPython 3.5.2, ``__aiter__`` can directly return
|
||||
:term:`asynchronous iterators <asynchronous iterator>`. Returning
|
||||
an :term:`awaitable` object will result in a
|
||||
:exc:`PendingDeprecationWarning`.
|
||||
|
||||
The recommended way of writing backwards compatible code in
|
||||
CPython 3.5.x is to continue returning awaitables from
|
||||
``__aiter__``. If you want to avoid the PendingDeprecationWarning
|
||||
and keep the code backwards compatible, the following decorator
|
||||
can be used::
|
||||
|
||||
import functools
|
||||
import sys
|
||||
|
||||
if sys.version_info < (3, 5, 2):
|
||||
def aiter_compat(func):
|
||||
@functools.wraps(func)
|
||||
async def wrapper(self):
|
||||
return func(self)
|
||||
return wrapper
|
||||
else:
|
||||
def aiter_compat(func):
|
||||
return func
|
||||
|
||||
Example::
|
||||
|
||||
class AsyncIterator:
|
||||
|
||||
@aiter_compat
|
||||
def __aiter__(self):
|
||||
return self
|
||||
|
||||
async def __anext__(self):
|
||||
...
|
||||
|
||||
Starting with CPython 3.6, the :exc:`PendingDeprecationWarning`
|
||||
will be replaced with the :exc:`DeprecationWarning`.
|
||||
In CPython 3.7, returning an awaitable from ``__aiter__`` will
|
||||
result in a :exc:`RuntimeError`.
|
||||
Starting with Python 3.7, ``__aiter__`` must return an
|
||||
asynchronous iterator object. Returning anything else
|
||||
will result in a :exc:`TypeError` error.
|
||||
|
||||
|
||||
.. _async-context-managers:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue