Skip to content

Warn when fixture loop scopes differ - #1552

Open
sunilkumarvalmiki wants to merge 1 commit into
pytest-dev:mainfrom
sunilkumarvalmiki:contrib/lane-d-loop-scope-warning
Open

Warn when fixture loop scopes differ#1552
sunilkumarvalmiki wants to merge 1 commit into
pytest-dev:mainfrom
sunilkumarvalmiki:contrib/lane-d-loop-scope-warning

Conversation

@sunilkumarvalmiki

Copy link
Copy Markdown

Summary

  • warn when an async fixture is requested by a test or async fixture using a different event-loop scope
  • add regression coverage for test-to-fixture and fixture-to-fixture mismatches
  • add the changelog entry and keep the subprocess warning check scoped to ResourceWarning

Tests

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

@tjkuson tjkuson left a comment

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.

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.

Comment thread pytest_asyncio/plugin.py
marker = request.node.get_closest_marker("asyncio")
if marker is None:
return None
return (

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.

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.

Comment thread pytest_asyncio/plugin.py
or fixturedef.scope
)
loop_scope = _get_fixture_loop_scope(fixturedef, request.config)
_warn_if_fixture_loop_scope_mismatch(fixturedef, request, loop_scope)

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.

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.

Comment thread pytest_asyncio/plugin.py
) -> 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(

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.

As written, this doesn't work with pytest-asyncio in auto mode. The warning should work on both auto and non-auto modes.

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.

2 participants