Skip to content

Deprecate @pytest_asyncio.fixture for synchronous fixtures#1473

Open
SaInekK wants to merge 1 commit into
pytest-dev:mainfrom
SaInekK:worktree-fix+typevar-r-widening
Open

Deprecate @pytest_asyncio.fixture for synchronous fixtures#1473
SaInekK wants to merge 1 commit into
pytest-dev:mainfrom
SaInekK:worktree-fix+typevar-r-widening

Conversation

@SaInekK
Copy link
Copy Markdown
Contributor

@SaInekK SaInekK commented Jun 4, 2026

Using @pytest_asyncio.fixture on a synchronous function is a user mistake — synchronous fixtures should use @pytest.fixture. Previously this silently succeeded, making the error hard to diagnose.

This PR:

  • Widens _R = TypeVar("_R") (removes the Awaitable | AsyncIterator bound) so applying @pytest_asyncio.fixture to a sync function no longer raises a type error before the warning fires.
  • Emits PytestDeprecationWarning at decoration time when the decorated function is not a coroutine or async generator.
  • Adds tests for both the bare-decorator and factory-decorator paths.

Closes #1090

Widen _R TypeVar (removing the Awaitable|AsyncIterator bound) so that
@pytest_asyncio.fixture can be applied to synchronous functions without
a type error. Emit PytestDeprecationWarning at decoration time when the
decorated function is not a coroutine or async generator function.

The internal synchronizer wrapper in pytest_fixture_setup bypasses
_make_asyncio_fixture_function to avoid a false-positive warning.

Closes pytest-dev#1090
@SaInekK SaInekK force-pushed the worktree-fix+typevar-r-widening branch from 6a90da5 to 03c9ab8 Compare June 4, 2026 23:40
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.54%. Comparing base (7817bdf) to head (03c9ab8).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1473      +/-   ##
==========================================
+ Coverage   94.50%   94.54%   +0.03%     
==========================================
  Files           2        2              
  Lines         510      513       +3     
  Branches       62       63       +1     
==========================================
+ Hits          482      485       +3     
  Misses         22       22              
  Partials        6        6              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants