-
-
Notifications
You must be signed in to change notification settings - Fork 203
fix async_generator error by making the app fixture async#336
fix async_generator error by making the app fixture async #336john0isaac wants to merge 8 commits into
Conversation
We are yielding the app fixture but not passing it as an argument which seems to break the second part of the documentation where we attempt to access the `test_client()` it gives an async_generator. ```bash AttributeError: 'async_generator' object has no attribute 'test_client' ```
pgjones
commented
May 19, 2024
Does this depend on the pytest-asyncio mode you are using? As it works for me without this change here
@pgjones I'm using the default values for everything.
I will reproduce and get back to you with the detailed versions of everything in an isolated environment.
This is my working setup here.
Have you tested this recently? Maybe it changed in the latest versions but was working previously..
My change works on Python 3.10, 3.11, 3.12
but I will reproduce again and leave detailed comment with it.
@pgjones I looked further into your setup and found the reason it works for you. Your project has asyncio_mode = "auto" configured in pyproject.toml:
[tool.pytest.ini_options] asyncio_mode = "auto"
With asyncio_mode = "auto", pytest-asyncio automatically converts coroutines and async generator functions declared with @pytest.fixture to pytest-asyncio fixtures. Without it, the default mode is strict, which only evaluates async fixtures decorated with @pytest_asyncio.fixture so @pytest.fixture on an async def function does not await the coroutine and returns the raw async_generator object instead, causing:
AttributeError: 'async_generator' object has no attribute 'test_client'
So there are actually two valid fixes for users hitting this error:
- Use
@pytest_asyncio.fixtureinstead of@pytest.fixture(this PR) - Add
asyncio_mode = "auto"to[tool.pytest.ini_options]in your pytest config
Since the Quart docs don't mention the asyncio_mode setting, most users following them will hit this error. This PR addresses that by making the decorator explicit.
urucoder
commented
Jan 8, 2025
I can confirm that I faced the same issue following the documentation and this simple fix solved it
@pgjones sorry I missed something when I left the last comment and I went back to try to understand this more I now know that my original comment didn't make sense so sorry for that
I edited my comment with the correct explanation of the issue and why it is working for you in your sample and not working here I missed checking your pytest setup in pyproject.toml
Uh oh!
There was an error while loading. Please reload this page.
The app fixture is not created as an async fixture which results in breaking the second part of the documentation where we attempt to access the
test_client()it gives an async_generator.This PR fixes that to pass the tests
Checklist:
CHANGES.rstsummarizing the change and linking to the issue... versionchanged::entries in any relevant code docs.pre-commithooks and fix any issues.pytestandtox, no tests failed.