agronholm / agronholm/anyio

Sync methods do not trigger async fixtures even if pytest.mark.anyio annotated

Open
#803 8 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
2.5k
Forks
260
Avg merge
2d 8h
Merged PRs (30d)
19

Description

### Things to check first

- [X] I have searched the existing issues and didn't find my bug already reported there

- [X] I have checked that my bug is still present in the latest release

### AnyIO version

4.6.0

### Python version

3.12

### What happened?

When trying to use this library with https://github.com/schireson/pytest-alembic (that has sync tests calling the async code https://pytest-alembic.readthedocs.io/en/latest/asyncio.html) I noticed that if you have an async fixture for a sync test, the fixture is not evaluated and instead the coroutine is inserted as value for the fixture.

### How can we reproduce the bug?

```python
from pytest_alembic import Config, MigrationContext
from pytest_alembic.tests import test_upgrade as upgrade

@pytest.fixture(scope="session") # must be session scoped to support sessions scooped DB setup
def anyio_backend() -> tuple[str, dict[str, bool]]:
return "asyncio", {"use_uvloop": True}

@pytest.fixture(scope="session", name="db")
async def _setup_db() -> None:
if await database_exists(ENGINE.url): # pragma: no branch
await drop_database(ENGINE.url) # pragma: no cover
await setup_db()

@pytest.mark.usefixtures("db")
async def test_upgrade(alembic_runner: MigrationContext) -> None: # noqa: RUF029 # fixture is async
upgrade(alembic_runner)

```
(Note I also ran into https://github.com/schireson/pytest-alembic/issues/119 when debugging this)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.