Skip to content

Conversation

@youknowone
Copy link
Member

@youknowone youknowone commented Jan 29, 2026

Summary by CodeRabbit

  • New Features

    • More explicit awaitable resolution and validation for await/yield operations, improving handling of objects that provide awaitables.
  • Bug Fixes

    • Clearer, more specific error messages when non-awaitable objects are used in async contexts (including async with).
    • Prevents awaiting the same coroutine more than once, reducing runtime surprises and improving async-with robustness.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 29, 2026

📝 Walkthrough

Walkthrough

GetAwaitable was changed from a unit opcode to carry an explicit argument; codegen emits GetAwaitable { arg: N }, VM/frame execution resolves awaitables via a new get_awaitable_iter() helper and uses the arg to contextualize error handling and awaitable validation.

Changes

Cohort / File(s) Summary
Instruction Definition
crates/compiler-core/src/bytecode/instruction.rs
Changed Instruction::GetAwaitable from a unit variant to GetAwaitable { arg: Arg<u32> }; updated matching, stack effect, and formatting to include the arg.
Code Generation
crates/codegen/src/compile.rs
Updated emission sites to emit `GetAwaitable { arg: <0
Awaitable Resolution Utility
crates/vm/src/coroutine.rs
Added pub fn get_awaitable_iter(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult to obtain/validate an awaitable iterator (checks coroutine, calls __await__, ensures iterator).
Frame Execution
crates/vm/src/frame.rs
Refactored execution of GetAwaitable { arg } to pop the value, call get_awaitable_iter(), validate the returned iterator (and coroutine reuse), and produce arg-specific TypeError messages for async-with contexts.

Sequence Diagram(s)

sequenceDiagram
    participant Codegen as Codegen/Compile
    participant Frame as VM Frame Executor
    participant AwaitUtil as get_awaitable_iter()
    participant Object as Python Object

    Codegen->>Frame: Emit GetAwaitable { arg: context }
    Frame->>Object: Pop awaitable-candidate
    Frame->>AwaitUtil: get_awaitable_iter(obj, vm)
    AwaitUtil->>Object: Is coroutine? / call __await__()
    alt Is coroutine
        Object-->>AwaitUtil: return coroutine (iterator-like)
    else __await__ exists
        Object-->>AwaitUtil: return iterator
        AwaitUtil->>AwaitUtil: validate iterator (not coroutine reuse)
    end
    AwaitUtil-->>Frame: awaitable iterator or error
    Frame->>Frame: push iterator / raise contextual TypeError
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested reviewers

  • ShaharNaveh

Poem

🐰 Hop, hop—an arg now on the trail,

I fetch the awaitable, swift and hale.
From compile to frame the pathways hum,
Iterators dance; the async work is done. ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Align GetAwaitable to Python 3.14.2' directly and clearly describes the main change: updating the GetAwaitable instruction to align with Python 3.14.2, which is the primary objective evident from the file modifications.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@youknowone youknowone marked this pull request as ready for review January 29, 2026 22:20
@youknowone youknowone marked this pull request as draft January 29, 2026 23:07
@github-actions
Copy link
Contributor

📦 Library Dependencies

The following Lib/ modules were modified. Here are their dependencies:

[ ] lib: cpython/Lib/fractions.py
[ ] test: cpython/Lib/test/test_fractions.py (TODO: 1)

dependencies:

  • fractions (native: math, sys)
    • functools, numbers, operator, re

dependent tests: (13 tests)

  • fractions: test_buffer test_builtin test_compare test_float test_fractions test_itertools test_math test_numeric_tower test_operator test_os test_random test_statistics
    • statistics: test_signal

[ ] lib: cpython/Lib/asyncio
[ ] test: cpython/Lib/test/test_asyncio (TODO: 17)

dependencies:

  • asyncio (native: _overlapped, _pyrepl.console, _remote_debugging, _winapi, asyncio.tools, collections.abc, concurrent.futures, errno, itertools, math, msvcrt, sys, time)
    • ast (native: _ast, sys)
    • collections (native: _weakref, itertools, sys)
    • dataclasses (native: itertools, sys)
    • enum (native: builtins, sys)
    • io (native: _io, _thread, errno, sys)
    • logging (native: atexit, errno, logging.handlers, sys, time)
    • os (native: os.path, sys)
    • site (native: _io, builtins, errno, sys)
    • socket (native: _socket, sys)
    • subprocess (native: builtins, errno, sys, time)
    • tempfile (native: _thread, errno, sys)
    • traceback (native: collections.abc, itertools, sys)
    • weakref (native: _weakref, itertools, sys)
    • _colorize, argparse, contextlib, contextvars, functools, heapq, inspect, linecache, reprlib, selectors, signal, stat, struct, threading, types, warnings

dependent tests: (8 tests)

  • asyncio: test_asyncio test_builtin test_contextlib_async test_inspect test_logging test_os test_sys_settrace test_unittest

Legend:

  • [+] path exists in CPython
  • [x] up-to-date, [ ] outdated

@youknowone youknowone marked this pull request as ready for review January 30, 2026 01:45
@youknowone youknowone enabled auto-merge (squash) January 30, 2026 01:46
@youknowone youknowone disabled auto-merge January 30, 2026 02:13
@youknowone youknowone merged commit 8e9d591 into RustPython:main Jan 30, 2026
14 checks passed
@youknowone youknowone deleted the get-awaitable branch January 30, 2026 03:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant