Warn when fixture loop scopes differ - #1552
Conversation
tjkuson
left a comment
There was a problem hiding this comment.
Hi, thanks for the PR. I left some comments regarding the warning being skipped depending on how fixtures are requested, which is a problem as the purpose of the warning is to catch these subtle mismatches. It also reaches into pytest internals which we would ideally avoid.
| marker = request.node.get_closest_marker("asyncio") | ||
| if marker is None: | ||
| return None | ||
| return ( |
There was a problem hiding this comment.
For fixtures with a non-function scope, request.node refers to the scope's collector, not the test requesting the fixture. Thus, mismatches with the test’s loop scope are missed.
| or fixturedef.scope | ||
| ) | ||
| loop_scope = _get_fixture_loop_scope(fixturedef, request.config) | ||
| _warn_if_fixture_loop_scope_mismatch(fixturedef, request, loop_scope) |
There was a problem hiding this comment.
pytest_fixture_setup only runs when the fixture is first created, so later requests bypass this check. The check shouldn't depend on test order/trigger inconsistently.
| ) -> tuple[str, _ScopeName] | None: | ||
| parent_request = getattr(request, "_parent_request", None) | ||
| parent_fixturedef = getattr(parent_request, "_fixturedef", None) | ||
| if parent_fixturedef is not None and _is_asyncio_fixture_function( |
There was a problem hiding this comment.
As written, this doesn't work with pytest-asyncio in auto mode. The warning should work on both auto and non-auto modes.
Summary
ResourceWarningTests
.venv\\Scripts\\ruff.exe check pytest_asyncio/plugin.py tests/test_fixture_loop_scopes.py tests/test_set_event_loop.py.venv\\Scripts\\python.exe -m pytest tests/test_fixture_loop_scopes.py -q(13 passed).venv\\Scripts\\python.exe -m pytest -q(297 passed, 3 skipped)Refs #1514