# Fix "AttributeError: module 'asyncio' has no attribute 'coroutine'"

Yes. An agent rewrites generator-based coroutines to async def, removes the loop arguments newer Python rejects, runs the tests and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/fix-asyncio-coroutine-attribute-error/asyncio/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent counts before it edits, because this error is the first symptom of a larger port. In an AMQP client like benjamin-hodgson/asynqp, `@asyncio.coroutine` decorates 22 functions across six modules under `src/asynqp`, with eight in `queue.py` alone, and those functions contain 30 `yield from` expressions. The tests add ten more decorated helpers, the README example one, and the two scripts in `doc/examples` four. Python 3.11 removed the decorator, so the package fails while `queue.py` is still being imported, at the first decorated method of the `Queue` class.

It reproduces the failure in a clean environment and reads the first traceback, which is not always the one in the title. `src/asynqp/spec.py` does `import pkg_resources` on line 5 to locate its bundled XML file, and a fresh environment with a current setuptools fails there first with `No module named 'pkg_resources'`. The agent reports that as a separate defect with its own fix, replaces it with `importlib.resources` only when the prompt allows, and otherwise pins setuptools for the test run so it can reach the error it was asked about.

The syntax change is mechanical: each `@asyncio.coroutine` and `def` pair becomes `async def`, and each `yield from` inside one becomes `await`. The agent checks every decorated function for a plain `yield`, which would turn it into an async generator, and for callers that iterate over the result. Then it runs the tests and meets the second removal. `protocol.py` passes `loop=self.loop` to `asyncio.sleep` in both heartbeat tasks, and `test/base_contexts.py` passes `loop=` to `asyncio.wait_for`. Python 3.10 dropped that parameter, so both raise `TypeError: sleep() got an unexpected keyword argument 'loop'` or its `wait_for` equivalent. `asyncio.Future(loop=...)` and `asyncio.ensure_future(..., loop=...)` still accept it and stay.

Tests need judgment too. The suite runs under the `contexts` framework, per `requirements.txt`, and `.travis.yml` starts RabbitMQ as a service because `test/integration_tests.py` connects to a broker on port 5672. The agent runs the unit test files one at a time first, since two integration contexts call `asyncio.set_event_loop(None)` and a failed connection leaves that in place for whatever runs next. It reports unit and integration results separately and says plainly when it had no broker.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "This project fails on Python 3.11 and later with: AttributeError: module 'asyncio' has no attribute 'coroutine'. Port it to native coroutines. First install the project in a clean virtual environment on the newest Python available and quote the first traceback you get; if it is a different error (for example a missing pkg_resources), report it, work around it for the test run only, and list it as a separate issue. Count every @asyncio.coroutine and every yield from in the package, the tests, the docs and the README, per file. Rewrite each decorated function to async def and each yield from inside one to await. For every rewritten function check that it has no bare yield and that no caller iterates over it or checks it with asyncio.iscoroutinefunction in a way the change breaks. Then find every asyncio call that passes loop=: remove the argument where Python 3.10 removed the parameter (sleep, wait, wait_for, gather, Queue, Lock, Event, Semaphore, Condition) and keep it where it is still accepted, and say which lines you changed. Do not change public method names or signatures. Run the unit tests file by file, then the whole suite; if integration tests need a service that is not running, say so and report them separately instead of counting them as failures of the port. Update the README and example scripts to the new syntax, and the supported-version metadata to the lowest Python that has async def. Open a pull request with per-file counts and the test summaries.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request that touches six package modules, the test helpers, the README and both example scripts. Its description opens with the per-file counts before and after, so a reviewer can confirm that no decorator or `yield from` was missed, and then lists the lines where a `loop=` argument was removed and the ones where it was kept. The test section gives the unit result for each test file and a separate line for the integration contexts with the reason they did or did not run.

The description also states what the port drops. `async def` needs Python 3.5, and the fixture's `setup.py` classifiers and CI matrix still name 3.4, so those entries go and the changelog says so. Anything the agent found on the way, such as the `pkg_resources` import, appears under its own heading with a proposed fix and is not folded into the diff unless the prompt asked for it.

A reviewer reads two things closely. The first is any function that was a coroutine only by decoration and never awaited anything: after the rewrite it must still be awaited by its callers. The second is the heartbeat code, where the removed `loop` argument means the sleep now runs on the running loop. In a library that lets callers pass their own loop to `connect`, the reviewer confirms those two are the same loop on every path.

## Run it on every Linear issue labelled python-upgrade

Teams usually meet this error while moving a set of services to a newer Python, one ticket per repository. An automation in each repository's Project with a `linear` trigger on `linear.issue.labeled`, filtered to a label such as `python-upgrade`, starts a run when someone on the team adds the label. The issue is placed after the saved prompt as event text, so the ticket can carry the target version and the failing command, while the prompt stays the only instruction. The trigger needs a Linear connection for whoever last edited the automation, made in the console under Settings → Connections, and a label added by an app integration does not fire it.

For a large codebase, map a workflow state to `plan` mode first: that run is read-only and posts its plan as a comment on the issue, with the per-file counts, before anyone commits to the port. Moving the issue to a state mapped to `exec` then starts the run that does the work.

## Why run it on Rush?

This port fails in layers, and each layer shows only after the one before it is fixed: a packaging import, then the decorator, then the `loop` arguments, then whatever the tests themselves assumed about event loops. Working through that needs a clean environment per attempt and a record of which error came from which change. The run has its own sandbox for that, and the transcript keeps every traceback in order, which is the evidence a reviewer needs to trust a diff that touches every coroutine in the package.

It also needs things a laptop may not have ready, such as a second interpreter to confirm the old behaviour and a message broker for the integration contexts. The agent installs what the sandbox allows and is explicit about what it could not run. A pull request that says the integration tests were not exercised is more useful than one that reports a green suite with those tests silently skipped.

## Frequently asked questions

### Is replacing the decorator with async def always safe?

For functions that only use `yield from` on awaitables, yes: the rewrite to `async def` and `await` is one for one. Two cases need care. A function with a bare `yield` becomes an async generator, and code that called a decorated plain function and relied on the decorator to make it awaitable now gets whatever the function returns. The agent checks for both.

### Why do I get a TypeError about loop after fixing the decorator?

Because the two removals arrived one release apart. The `loop` parameter of `asyncio.sleep`, `wait_for`, `gather`, `Queue`, `Lock` and similar calls was deprecated in Python 3.8 and removed in 3.10, and the `coroutine` decorator went in 3.11. Code old enough to use the decorator usually passes `loop` as well, and the import error simply hides the second problem until the first is fixed.

### Can the code stay compatible with Python 3.4?

Not with native coroutines, since `async def` and `await` are syntax errors there. Keeping 3.4 would mean keeping the decorator, which cannot run on 3.11. The agent sets the supported floor to the lowest version with `async def` and removes older entries from the metadata and CI matrix.

### What if the error comes from a dependency and not my code?

The agent reads the traceback's top frame. When it is inside an installed package, it looks for a release of that package that uses `async def`, bumps the requirement and runs your tests. When the package was never ported, it reports the options it found, a maintained fork or a different client library, and does not rewrite the installed files.

## More asyncio jobs

- [Fix "TypeError: As of 3.10, the *loop* parameter was removed from Lock()" in asyncio](https://getrush.ai/run/fix-asyncio-loop-parameter-removed/asyncio/)
- [Fix "RuntimeError: There is no current event loop" on Python 3.14](https://getrush.ai/run/fix-no-current-event-loop-runtime-error/asyncio/)
- [Fix "AttributeError: type object '_asyncio.Task' has no attribute 'all_tasks'" on Python 3.9 and Later](https://getrush.ai/run/fix-task-all-tasks-attribute-error/asyncio/)

## More jobs like this: Turn a Linear label into a PR

- [Fix the body-parser "provide extended option" Deprecation Warning in Express](https://getrush.ai/run/fix-body-parser-extended-option-deprecation/body-parser/)
- [Fix "AttributeError: module 'cgi' has no attribute 'escape'" on Python 3.8 and Later](https://getrush.ai/run/fix-cgi-escape-attribute-error/cgi/)
- [Fix "Deprecated: Creation of dynamic property is deprecated" in PHP 8.2](https://getrush.ai/run/fix-creation-of-dynamic-property-deprecated/php/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
