Skip to content

Type error: Value of type variable "_R" of function cannot be "AsyncClient" #1090

Closed

Description

@jamesbraza

Upgrading to pytest-asyncio==0.26.0:

import pytest_asyncio
from httpx import AsyncClient


@pytest_asyncio.fixture(scope="function")
def server_async_client() -> AsyncClient: ...

I get this error from mypy:

a.py:5:2: error: Value of type variable "_R" of function cannot be "AsyncClient"  [type-var]
    @pytest_asyncio.fixture(scope="function")
     ^

This is with Python 3.12.7 and the following packages:

httpx                     0.27.2
mypy                      1.15.0
pytest                    8.3.5
pytest-asyncio            0.25.3

Activity

  1. seifertm commented on Apr 16, 2025

    @seifertm
    Contributor

    Thanks for reporting this! Could this be a regression of #1045?

  2. added theissue type on Apr 16, 2025
  3. added this to the v0.26 milestone on Apr 16, 2025
  4. jamesbraza commented on Apr 16, 2025

    @jamesbraza
    Author

    Yeah correct, that PR binds the "return" TypeVar _R to be awaitable, assuming it's always wrapping an async function.

    That assumption is incorrect because it's possible to wrap non-async functions with pytest_asyncio.fixture:

    @pytest_asyncio.fixture
    def some_int() -> int:
        return 5

    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:

    @pytest.fixture
    def some_int() -> int:
        return 5
  5. seifertm commented on Apr 17, 2025

    @seifertm
    Contributor

    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.

  6. chrisk314 commented on May 6, 2025

    @chrisk314

    This is not limited to wrapping non-async functions. I'm also seeing this kind of error when wrapping an
    async iterable as below.

    import asyncio
    from typing import AsyncIterable
    
    import pytest_asyncio
    
    
    @pytest_asyncio.fixture(scope="session")
    async def dummy_fixture() -> AsyncIterable[str]:
        await asyncio.sleep(1)
        yield "dummy"

    Running mypy on this code produces:

    error: Value of type variable "_R" of function cannot be "AsyncIterable[str]"  [type-var]
    
  7. modified the milestones: v0.26, v1.1 on May 26, 2025
  8. 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
  9. removed this from the v1.1 milestone on Sep 12, 2025
  10. added a commit that references this issue on Jun 4, 2026
    03c9ab8
  11. seifertm commented on Jun 13, 2026

    @seifertm
    Contributor

    Yeah correct, that PR binds the "return" TypeVar _R to be awaitable, assuming it's always wrapping an async function.

    That assumption is incorrect because it's possible to wrap non-async functions with pytest_asyncio.fixture:

    @pytest_asyncio.fixture
    def some_int() -> int:
    return 5

    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:

    @pytest.fixture
    def some_int() -> int:
    return 5

    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.

    import asyncio
    from typing import AsyncIterable

    import pytest_asyncio

    @pytest_asyncio.fixture(scope="session")
    async def dummy_fixture() -> AsyncIterable[str]:
    await asyncio.sleep(1)
    yield "dummy"

    Running mypy on this code produces:

    error: Value of type variable "_R" of function cannot be "AsyncIterable[str]"  [type-var]
    

    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).

  12. added this to the v1.4 milestone on Jun 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions