Skip to content
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

Enhance aiter to support built-in sync iterables #101495

Closed
Henkhogan opened this issue Feb 1, 2023 · 15 comments
Closed

Enhance aiter to support built-in sync iterables #101495

Henkhogan opened this issue Feb 1, 2023 · 15 comments
Labels
topic-asyncio type-feature A feature request or enhancement

Comments

@Henkhogan
Copy link

Henkhogan commented Feb 1, 2023

Feature or enhancement

Enhance builtin iterables like list, range with async methods like aiter, anext,...

Pitch

It would allow to use e.g. async list comprehenions on normal lists like:

[_ async for _ in list(range(1,10))]

-> helpful if the code snipped is supposed to handle async iterables like normal iterables

Previous discussion

https://discuss.python.org/t/enhance-builtin-iterables-like-list-range-with-async-methods-like-aiter-anext/21352/1

Linked PRs

@Henkhogan Henkhogan added the type-feature A feature request or enhancement label Feb 1, 2023
@ofek
Copy link
Contributor

ofek commented Feb 1, 2023

I am very much in favor of this as it would remove the need to pepper the use of the following utility throughout every code base:

async def aiter(iterable: Iterable):
    for item in iterable:
        yield item

@zware
Copy link
Member

zware commented Feb 1, 2023

As mentioned in the discussion, sprinkling __aiter__ implementations around the codebase is not the way to go here. Adjusting the aiter builtin to automatically wrap synchronous iterators is a much more palatable option.

@ethanfurman
Copy link
Member

Is adding __aiter__ to object feasible?

@brettcannon
Copy link
Member

Is adding __aiter__ to object feasible?

Feasible, yes. Desirable, no, as that would make every object an asynchronous iterable.

@kumaraditya303
Copy link
Contributor

I am -1 on this, to me this feels like an anti pattern and if you are using async generator for sync code then IMO it is wrong design.

@merwok
Copy link
Member

merwok commented Feb 3, 2023

Some motivation was provided in the thread, and the current proposal is about changing aiter rather than built-in objects.

@ofek
Copy link
Contributor

ofek commented Feb 3, 2023

Yes, can we change the PR title?

@merwok merwok changed the title Enhance builtin iterables like list, range with async methods like __aiter__, __anext__,... Enhance aiter to support built-in sync iterables Feb 3, 2023
@Henkhogan
Copy link
Author

Should this be extended to "async with" which seems very similar to me

@zware
Copy link
Member

zware commented Feb 3, 2023

Should this be extended to "async with" which seems very similar to me

Not in this issue, but perhaps in another. It has different considerations and is completely unrelated to this issue.

@SimpleArt
Copy link

SimpleArt commented Feb 5, 2023

It is worth noting that async-stdlib includes this function, which can serve as a reference implementation.

This does however beg me the question: is it aiter that needs to be changed or the semantics of using async for? It sounds like we're going from async for x in iterable to using async for x in aiter(iterable) everywhere. This situation is also similar to iter(...) and types which implement __getitem__ and __len__. Note that there's no need to do for x in iterable in those cases either. Ideally if async for itself is changed, then one can simply think of aiter as async for x in iterable: yield x, so it should resolve this case as well.

@merwok
Copy link
Member

merwok commented Feb 5, 2023

In general, Python’s design is that keyword and operators have clear, simple behaviour (async for iterates over async iterables) and functions can do convenient things and conversions (iter is more than just calling __iter__, aiter can wrap sync iterables).

@brettcannon
Copy link
Member

It is worth noting that async-stdlib includes this function, which can serve as a reference implementation.

There are potential licensing concerns unless the person who wrote that code for async-stdlib also opens the PR to contribute the code to CPython.

is it aiter that needs to be changed or the semantics of using async for?

It's aiter.

It sounds like we're going from async for x in iterable to using async for x in aiter(iterable) everywhere.

That's only true if you accept any iterable, async or not, and want it to work in that loop.

@kumaraditya303
Copy link
Contributor

The discussion died and I am still strong -1 to this, I suggest anyone interested taking this forward to get agreement on the semantics and work on a possible PEP. Closing as it is not actionable now.

@kumaraditya303 kumaraditya303 closed this as not planned Won't fix, can't repro, duplicate, stale Apr 19, 2023
@github-project-automation github-project-automation bot moved this from Todo to Done in asyncio Apr 19, 2023
@lambdaq
Copy link

lambdaq commented Nov 2, 2023

Some motivation was provided in the thread, and the current proposal is about changing aiter rather than built-in objects.

This sounds like a good compromise, are there any shipping plans to update the built-in aiter() ?

@mjsir911
Copy link

@ericvsmith This is the issue (talking about this in pycon), probably will not open a pretty much duplicate issue of this feature proposal. Will pursue a PEP as per @kumaraditya303's suggestion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-asyncio type-feature A feature request or enhancement
Projects
Status: Done
Development

No branches or pull requests