You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Type error: Value of type variable "_R" of function cannot be "AsyncClient" #1090
An alternate design could be pytest_asyncio.fixture blowing up if not wrapping an async function, as otherwise one should just use a normal pytest.fixture:
In my opinion, pytest-asyncio should raise an error when the user tries to decorate synchronous tests with pytest_asyncio.fixture. There should only be one way to decorate those fixtures and pytest already provides it.
That said, we cannot simply error out in this case. We should first raise a DeprecationWarning to give users a chance to update their code. A later release can turn the warning into an error.
changed the title [-]Type error in `pytest-asyncio==0.26.0` with: `Value of type variable "_R" of function cannot be "AsyncClient"`[/-][+]Type error: `Value of type variable "_R" of function cannot be "AsyncClient"`[/+]on May 26, 2025
An alternate design could be pytest_asyncio.fixture blowing up if not wrapping an async function, as otherwise one should just use a normal pytest.fixture:
I understand that this issue has been reported as a regression in v0.26. However, the @pytest_asyncio.fixture decorator is not intended to be applied to synchronous fixtures. Users should rely on @pytest.fixture, instead.
While applying @pytest_asyncio.fixture to synchronous fixtures may work normally, the decorator nowadays provides additional keyword arguments, such as loop_scope, which are not applicable to synchronous fixtures. This is why the type signature only accounts for async fixture functions.
This is not limited to wrapping non-async functions. I'm also seeing this kind of error when wrapping an async iterable as below.
Activity
seifertm commentedon Apr 16, 2025
Thanks for reporting this! Could this be a regression of #1045?
jamesbraza commentedon Apr 16, 2025
Yeah correct, that PR binds the "return"
TypeVar_Rto be awaitable, assuming it's always wrapping anasyncfunction.That assumption is incorrect because it's possible to wrap non-
asyncfunctions withpytest_asyncio.fixture:An alternate design could be
pytest_asyncio.fixtureblowing up if not wrapping anasyncfunction, as otherwise one should just use a normalpytest.fixture:seifertm commentedon Apr 17, 2025
In my opinion, pytest-asyncio should raise an error when the user tries to decorate synchronous tests with
pytest_asyncio.fixture. There should only be one way to decorate those fixtures and pytest already provides it.That said, we cannot simply error out in this case. We should first raise a DeprecationWarning to give users a chance to update their code. A later release can turn the warning into an error.
chrisk314 commentedon May 6, 2025
This is not limited to wrapping non-async functions. I'm also seeing this kind of error when wrapping an
async iterable as below.
Running mypy on this code produces:
[-]Type error in `pytest-asyncio==0.26.0` with: `Value of type variable "_R" of function cannot be "AsyncClient"`[/-][+]Type error: `Value of type variable "_R" of function cannot be "AsyncClient"`[/+]Deprecate @pytest_asyncio.fixture for synchronous fixtures
seifertm commentedon Jun 13, 2026
I understand that this issue has been reported as a regression in v0.26. However, the
@pytest_asyncio.fixturedecorator is not intended to be applied to synchronous fixtures. Users should rely on@pytest.fixture, instead.While applying
@pytest_asyncio.fixtureto synchronous fixtures may work normally, the decorator nowadays provides additional keyword arguments, such asloop_scope, which are not applicable to synchronous fixtures. This is why the type signature only accounts for async fixture functions.This is still reproducible under pytest-asyncio v1.4.0.
AsyncIterable should be included in the type bound for TypeVar
_R(see https://docs.python.org/3.14/library/typing.html#annotating-generators-and-coroutines).