You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Warn when a fixture for a different loop_scope is requested #1514
Pytest-asyncio currently allows requesting fixtures that have a different loop_scope than the requesting test or fixture. Since fixtures with different loop scopes use different asyncio event loops, this can lead to unexpected errors., e.g. trying to access a task that was created on a different loop.
This can happen when:
a test requests a fixture, but the loop_scopes don't match
a fixture requests another fixture, but the loop_scopes don't match
Mixing fixtures with different loop scopes is almost certainly a user error and should result in a warning.
Work on this issue was done during the pytest sprint 2026, sponsored by Omicron.
Emitting a warning when pytest 'jumps' a loop scope during the execution of a test and its fixtures makes a lot of sense to me. However, users of pytest-asyncio currently can configure the default loop scope for fixtures and tests separately. I think it also follows that it is almost certain that configuring the default loop scopes for fixtures and tests to be different is likely a user error as well.
To avoid user confusion, I think it would make sense to consolidate these configs to a single asyncio_default_loop_scope that controls the default loop scope for both fixtures and tests. We could then deprecate the current configurations and point people to use the new one. The way this would be communicated to users could be along the lines of:
pytest-asyncio defaults to running each test function and any async fixture it requests in its own function-scoped event loop. If most of your async tests use async fixtures that use a wider loop scope (e.g., a session-scoped async fixture that requires a session-scoped event loop), you should configure the default loop scope accordingly. You can control the loop scope on individual test functions and fixtures using the pytest.mark.asyncio marker and pytest_asyncio.fixture decorator.
Activity
Add PytestAsyncioLoopScopeMismatchWarning for loop-scope mismatches (p…
tjkuson commentedon Jul 24, 2026
Emitting a warning when pytest 'jumps' a loop scope during the execution of a test and its fixtures makes a lot of sense to me. However, users of pytest-asyncio currently can configure the default loop scope for fixtures and tests separately. I think it also follows that it is almost certain that configuring the default loop scopes for fixtures and tests to be different is likely a user error as well.
To avoid user confusion, I think it would make sense to consolidate these configs to a single
asyncio_default_loop_scopethat controls the default loop scope for both fixtures and tests. We could then deprecate the current configurations and point people to use the new one. The way this would be communicated to users could be along the lines of: