If you look at
https://github.com/python/cpython/blob/b11a951f16f0603d98de24fee5c023df83ea552c/Python/ceval.c#L2409-L2451
you will see that `async for` requires that the iterator returned from
`__aiter__` define `__anext__`. But if you look at aiter() which uses
PyObject_GetAiter() from
https://github.com/python/cpython/blob/f0a6fde8827d5d4f7a1c741ab1a8b206b66ffd57/Objects/abstract.c#L2741-L2759
and PyAiter_Check() from
https://github.com/python/cpython/blob/f0a6fde8827d5d4f7a1c741ab1a8b206b66ffd57/Objects/abstract.c#L2769-L2778
you will notice that aiter() requires `__anext__` *and* `__aiter__` on the
async iterator that gets returned from __aiter__.

Now the docs for aiter() at
https://docs.python.org/3.10/library/functions.html#aiter points out that
the async iterator is expected to define both methods as does the glossary
definition for "asynchronous iterator" (
https://docs.python.org/3.8/glossary.html#term-asynchronous-iterator).

So my question is whether the discrepancy between what `async for` expects
and what `aiter()` expects on purpose? https://bugs.python.org/issue31861
was the issue for creating aiter() and I didn't notice a discussion of this
difference. The key reason I'm asking is this does cause a deviation
compared to the relationship between `for` and `iter()` (which does not
require `__iter__` to be defined on the iterator, although
collections.abc.Iterator does). It also makes the glossary definition being
linked from
https://docs.python.org/3.10/reference/compound_stmts.html#the-async-for-statement
incorrect.
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/ZADNVL7SGH24QXBNPKMN33ELFSXRI2VJ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to