Title: Implement `anext` and `aiter` in pure Python · Issue #157361 · python/cpython · GitHub
Open Graph Title: Implement `anext` and `aiter` in pure Python · Issue #157361 · python/cpython
X Title: Implement `anext` and `aiter` in pure Python · Issue #157361 · python/cpython
Description: Feature or enhancement Feature or enhancement I propose to implement anext() and aiter() in Python instead of C, compiled at interpreter startup and copied into builtins. This removes the private anext_awaitable, async_callable_iterator ...
Open Graph Description: Feature or enhancement Feature or enhancement I propose to implement anext() and aiter() in Python instead of C, compiled at interpreter startup and copied into builtins. This removes the private a...
X Description: Feature or enhancement Feature or enhancement I propose to implement anext() and aiter() in Python instead of C, compiled at interpreter startup and copied into builtins. This removes the private a...
Opengraph URL: https://github.com/python/cpython/issues/157361
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Implement `anext` and `aiter` in pure Python","articleBody":"# Feature or enhancement\n\n## Feature or enhancement\n\nI propose to implement `anext()` and `aiter()` in Python instead of C, compiled at interpreter startup and copied into builtins. This removes the private `anext_awaitable`, `async_callable_iterator` and `async_callable_iterator_awaitable` types.\n\n`anext(it, default)` becomes a plain coroutine that awaits `type(it).__anext__(it)` and converts `StopAsyncIteration` into the default. The callable form of `aiter()` (gh-64862) becomes a small class with `async def __anext__` that calls and awaits the callable and turns the sentinel or `stop_exception` into `StopAsyncIteration`. Semantics stay the same.\n\nThe motivation is #157032 and #157044: the C awaitables break the asyncio await graph. `capture_call_graph` walks `cr_await`/`ag_await`, and these types have neither, so the graph stops at the frame calling `anext(it, default)` or iterating `aiter(callable, sentinel)` and hides where the task is actually suspended. This would fix #157032, #157044 without any special casing. \n\n\nRepro with a class-based async iterator:\n\n```python\nimport asyncio\n\nasync def fetch():\n await asyncio.sleep(10)\n\nclass AIter:\n def __aiter__(self): return self\n async def __anext__(self):\n await fetch()\n return 1\n\nasync def consumer():\n await anext(AIter(), None)\n\nasync def main():\n t = asyncio.create_task(consumer(), name=\"consumer\")\n for _ in range(3):\n await asyncio.sleep(0)\n asyncio.print_call_graph(t)\n t.cancel()\n\nasyncio.run(main())\n```\n\nBefore:\n\n```\n* Task(name='consumer', id=0x10a42c7d0)\n + Call stack:\n | File 'demo.py', line 13, in async consumer()\n```\n\nAfter:\n\n```\n* Task(name='consumer', id=0x108959b80)\n + Call stack:\n | File 'Lib/asyncio/tasks.py', line 707, in async sleep()\n | File 'demo.py', line 4, in async fetch()\n | File 'demo.py', line 9, in async AIter.__anext__()\n | File '\u003cbuiltins>', line 26, in async _anext_with_default()\n | File 'demo.py', line 13, in async consumer()\n```\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-157362\n* gh-157687\n* gh-159867\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/kumaraditya303","@type":"Person","name":"kumaraditya303"},"datePublished":"2026-09-12T10:30:18Z","url":"https://github.com/python/cpython/issues/157361"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:24f9064f-c91b-e411-7fa8-fa77620c85d3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A802:E9D16:3C2365D:4C2F299:6B023A98 |
| html-safe-nonce | aa033a3b85386c7a5e3f42974d1fdb897ada036925e8bcce86fdbfe9f9e6241f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBODAyOkU5RDE2OjNDMjM2NUQ6NEMyRjI5OTo2QjAyM0E5OCIsInZpc2l0b3JfaWQiOiI4NzA0NjUxMjY3NjAwOTU2MDU2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 5d25e31eafb520a63470a8508d881a71163ae1dfa4964d4be02ba825647cce54 |
| hovercard-subject-tag | issue:5432747176 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/157361/issue_layout |
| twitter:image | https://opengraph.githubassets.com/11ec0515315208e892582f52159c29e934fac0dd5ce5e8c12320b005bad8ceae/python/cpython/issues/157361 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/11ec0515315208e892582f52159c29e934fac0dd5ce5e8c12320b005bad8ceae/python/cpython/issues/157361 |
| og:image:alt | Feature or enhancement Feature or enhancement I propose to implement anext() and aiter() in Python instead of C, compiled at interpreter startup and copied into builtins. This removes the private a... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kumaraditya303 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width