Skip to content

Replace uses of types.coroutine with a class#3266

Closed
TeamSpen210 wants to merge 3 commits into
python-trio:mainfrom
TeamSpen210:class-yielder
Closed

Replace uses of types.coroutine with a class#3266
TeamSpen210 wants to merge 3 commits into
python-trio:mainfrom
TeamSpen210:class-yielder

Conversation

@TeamSpen210
Copy link
Copy Markdown
Contributor

After seeing a discuss.python.org thread about finally deprecating types.coroutine, I thought I'd try pre-emptively removing the two internal places in Trio which uses it. Both are to yield one value, to terminate the await stack. Also removed a typing.cast(), though we could have actually fixed that without the class change.

Instead of a class, we could also just copy over the code that sets the relevant bitflag in the code object, but this is much more robust and future proof. I imagine that flag will also be removed at some point.

@oremanj since you just posted in that thread.

@oremanj
Copy link
Copy Markdown
Member

oremanj commented May 14, 2025

This is a valid workaround. I think it is less clear and probably slower, though. I would prefer not to make the change unless there is an actual improvement from it or upstream removes the capability we’re currently relying on. I still think the latter would be a mistake, but it’s outside of our control.

@TeamSpen210
Copy link
Copy Markdown
Contributor Author

I did try a few micro-benchmarks (running a few million checkpoint()s), the difference was tiny. Adding a few dummy calls to int() was enough to cancel it out for example.

@A5rocks
Copy link
Copy Markdown
Contributor

A5rocks commented May 15, 2025

Could you get some concrete numbers? I think pyperf timeit works for microbenchmarks. (and then pyperf compare_to for a nice table)

Comment thread src/trio/_core/_traps.py
class _AsyncYield:
"""Helper for the bottommost 'yield'.

You can't use 'yield' inside an async function, so implement an awaitable object to do so.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use yield inside an async function! It just doesn't do what we want.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite true.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 15, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.93728%. Comparing base (bb63c53) to head (97c25fe).
⚠️ Report is 71 commits behind head on main.

Additional details and impacted files
@@              Coverage Diff              @@
##                main       #3266   +/-   ##
=============================================
  Coverage   99.93728%   99.93728%           
=============================================
  Files            126         126           
  Lines          19132       19133    +1     
  Branches        1296        1296           
=============================================
+ Hits           19120       19121    +1     
  Misses            10          10           
  Partials           2           2           
Files with missing lines Coverage Δ
src/trio/_core/_tests/test_run.py 100.00000% <100.00000%> (ø)
src/trio/_core/_traps.py 100.00000% <100.00000%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@A5rocks
Copy link
Copy Markdown
Contributor

A5rocks commented Jun 9, 2025

Let's close this until there's actually some consensus; we'll have time before any change lands and then arrives in a version we support.

@A5rocks A5rocks closed this Jun 9, 2025
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.

4 participants