fix: apply loop factories for asyncio marks via parametrize#1509
fix: apply loop factories for asyncio marks via parametrize#1509l46983284-cpu wants to merge 6 commits into
Conversation
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 Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
| ], | ||
| ) | ||
| async def test_async(backend: str) -> None: | ||
| assert type(asyncio.get_running_loop()).__name__ == "CustomEventLoop" |
There was a problem hiding this comment.
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>
|
Thanks for the review — agreed the first regression was too narrow for the #1463 multi-backend shape. I added 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>
|
|
||
|
|
||
| @pytest.hookimpl(tryfirst=True) | ||
| def pytest_pyfunc_call(pyfuncitem): |
There was a problem hiding this comment.
Thank you for adding the test in response to my feedback, but this hook implementation should not be present here. Similar to my previous comment, I do not think this meaningfully covers the reported issue.
Remove the test-local pytest_pyfunc_call runner that invented a second async backend. Keep the ParameterSet asyncio + skip + CustomEventLoop regression that covers the supported product slice for pytest-dev#1463. Signed-off-by: Alex Chen <l46983284@gmail.com>
|
Thanks — agreed. The local What remains is the part of #1463 this change can actually fix without pytest internals: if Happy to adjust further if you want a different regression shape. |
Summary
In strict mode,
pytest.mark.asynciocan be attached only to individual@pytest.mark.parametrizeparameter sets. Collection already turns those async parameter sets into Coroutine items, butpytest_generate_testsonly looked for a function-level asyncio marker. As a result,pytest_asyncio_loop_factorieswas 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.Scope
Supported slice only: discover
pytest.mark.asyncioon aParameterSetso loop factories apply, and the test body sees that loop.This does not implement multi-backend partial ownership (for example asyncio + trio on one function without affecting the other path). That still needs the public pytest API discussed in pytest#13233, same as on the issue.
Tests
python -m pytest tests/test_loop_factory_parametrization.py::test_asyncio_mark_via_parametrize_uses_hook_loop_factories -qFixes #1463