pytest-dev / pytest-dev/pytest-asyncio

loop_scope default

Open
#1,095 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.7k
Forks
207
Avg merge
5h 35m
Merged PRs (30d)
9

Description

(in relation to https://github.com/pytest-dev/pytest-asyncio/issues/706 and other related issues)

Can I get the fixture loop_scope to default to the fixture's scope?

Description

If I have a fixture that provides an async resource like so:

@pytest.fixture(scope='module')
async def my_async_fixture():
    yield my_async_resource()

I need to declare it with pytest_asyncio.fixture and define loop_scope to match the fixture scope (see 0.23 migration guide):

@pytest_asyncio.fixture(scope='module', loop_scope='module')
async def my_async_fixture():
    yield my_async_resource()

But the fixture scope is a safe and sensible default for the loop_scope in my work. Lesser scopes typically won't work because the point of the fixture is to manage an async resource (not just to run async code before/after the test). The point of higher scopes in general is to create resources that can be shared between tests, so we accept the isolation implications when we write them and are happy with loop_scope = scope as a default (we can always override it if we have a use case to).

This loop_scope parameter is easily omitted, and the pytest_asyncio.fixture decorator is less known to contributors making this pattern a bit of a pain point. Is there a way I can make the loop_scope default to the fixture scope so we can continue to use @pytest.fixture(scope=<scope>) as before?

Note, I can't use the asyncio_default_fixture_loop_scope because I have fixtures with session, module, class and function scopes.

Proposal

If this can't be done at present, would it make sense to provide a asyncio_default_fixture_loop_scope = scope option?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by examining the pytest_asyncio.fixture decorator and the existing asyncio_default_fixture_loop_scope configuration. Determine whether loop_scope can safely inherit each fixture's scope for session, module, class, and function fixtures, while preserving explicit overrides. Add coverage for the proposed default and verify the existing configuration behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.