Skip to content

Warn when a fixture for a different loop_scope is requested #1514

Description

@seifertm

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.

Activity

  1. added this to the v1.5 milestone on Jul 21, 2026
  2. added theissue type on Jul 21, 2026
  3. tjkuson commented on Jul 24, 2026

    @tjkuson
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions