Skip to content

fix: apply loop factories for asyncio marks via parametrize#1509

Open
l46983284-cpu wants to merge 5 commits into
pytest-dev:mainfrom
l46983284-cpu:fix/parametrize-asyncio-loop-factories
Open

fix: apply loop factories for asyncio marks via parametrize#1509
l46983284-cpu wants to merge 5 commits into
pytest-dev:mainfrom
l46983284-cpu:fix/parametrize-asyncio-loop-factories

Conversation

@l46983284-cpu

@l46983284-cpu l46983284-cpu commented Jul 16, 2026

Copy link
Copy Markdown

Summary

In strict mode, pytest.mark.asyncio can be attached only to individual @pytest.mark.parametrize parameter sets (for example multi-backend tests). Collection already turns those async parameter sets into Coroutine items, but pytest_generate_tests only looked for a function-level asyncio marker. As a result, pytest_asyncio_loop_factories was never applied and the test kept the default event loop.

This change resolves the asyncio marker from parametrize parameter sets when the function itself has no asyncio marker, so configured loop factories still parametrize _asyncio_loop_factory.

Tests

  • Regression: asyncio mark only via parametrize + custom loop factory (passed + skipped non-asyncio set).
  • Review residual: multi-backend parametrize with pytest.mark.asyncio and an analogous non-asyncio backend mark, asserting loop type in the test body (tests/test_loop_factory_parametrize_multi_backend.py). CI does not depend on pytest-trio; the second backend is a tiny inline plugin.
  • Local:
    python -m pytest tests/test_loop_factory_parametrization.py::test_asyncio_mark_via_parametrize_uses_hook_loop_factories tests/test_loop_factory_parametrize_multi_backend.py::test_asyncio_and_analogous_backend_parametrize_uses_loop_factories -q
  • House: python -m pyright pytest_asyncio/ clean on this head.

Fixes #1463

When pytest.mark.asyncio is attached only to individual parameter sets,
pytest_generate_tests still needs to resolve that marker so
pytest_asyncio_loop_factories can parametrize the event loop.

Fixes pytest-dev#1463

Signed-off-by: Alex Chen <l46983284@gmail.com>
Signed-off-by: Alex Chen <l46983284@gmail.com>
@codecov-commenter

codecov-commenter commented Jul 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 55.17241% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.39%. Comparing base (1975f90) to head (1e46cc2).
⚠️ Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
pytest_asyncio/plugin.py 55.17% 6 Missing and 7 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1509      +/-   ##
==========================================
- Coverage   94.50%   92.39%   -2.12%     
==========================================
  Files           2        2              
  Lines         510      539      +29     
  Branches       62       71       +9     
==========================================
+ Hits          482      498      +16     
- Misses         22       28       +6     
- Partials        6       13       +7     

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

],
)
async def test_async(backend: str) -> None:
assert type(asyncio.get_running_loop()).__name__ == "CustomEventLoop"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this test covers the failure described in #1463. I would expect this PR to include a test that is parametrized via both pytest.mark.asyncio and pytest.mark.trio (or something analogous) with an assertion on loop type within the test body.

Address review on pytest-dev#1509: assert loop type for asyncio and an analogous
non-asyncio backend mark in the same parametrize, matching the pytest-dev#1463 shape
without adding pytest-trio to CI deps.

Signed-off-by: Alex Chen <l46983284@gmail.com>
Move the pytest-dev#1509 review residual into its own test module so ruff format
does not thrash the large existing pytester file. Covers asyncio +
analogous non-asyncio backend marks with loop-type asserts (pytest-dev#1463 shape).

Signed-off-by: Alex Chen <l46983284@gmail.com>
@l46983284-cpu

Copy link
Copy Markdown
Author

Thanks for the review — agreed the first regression was too narrow for the #1463 multi-backend shape.

I added test_asyncio_and_analogous_backend_parametrize_uses_loop_factories: one parametrize with pytest.mark.asyncio and an analogous non-asyncio backend mark, and loop-type asserts inside the test body. CI no longer depends on pytest-trio, so the second backend is a tiny inline plugin instead of a hard trio dep; with pytest-trio installed locally the same product path also passes the real trio pair.

Focused tests green on this head.

Match pre-commit shed/black parentheses collapse on pytester.dedent calls.

Signed-off-by: Alex Chen <l46983284@gmail.com>
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.

pytest_asyncio_loop_factories hook is not called when pytest.mark.asyncio is added via parametrization

3 participants