pytest-dev / pytest-dev/pytest-asyncio

1.1.0: Session fixture with `asyncio_default_fixture_loop_scope=function` yields `You tried to access the function scoped fixture _function_scoped_runner with a session scoped request object`

Open
#1,175 10 comments 0 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

Hit this in the wild while trying to run fakeredis's test suite.

Trivial reproducer:

import pytest_asyncio


@pytest_asyncio.fixture(scope="session")
def foo():
    pass


def test_foo(foo):
    pass
$ pytest -o asyncio_default_fixture_loop_scope=function test_foo.py -vv
========================================================= test session starts =========================================================
platform linux -- Python 3.13.5, pytest-8.4.1, pluggy-1.6.0 -- /tmp/fakeredis-py/.venv/bin/python3
cachedir: .pytest_cache
hypothesis profile 'default'
rootdir: /tmp
plugins: asyncio-1.1.0, hypothesis-6.136.6
asyncio: mode=Mode.STRICT, debug=False, asyncio_default_fixture_loop_scope=function, asyncio_default_test_loop_scope=function
collected 1 item                                                                                                                      

test_foo.py::test_foo ERROR                                                                                                     [100%]

=============================================================== ERRORS ================================================================
_____________________________________________________ ERROR at setup of test_foo ______________________________________________________
ScopeMismatch: You tried to access the function scoped fixture _function_scoped_runner with a session scoped request object. Requesting fixture stack:
test_foo.py:4:  def foo()
Requested fixture:
pytest-asyncio/pytest_asyncio/plugin.py:774:  def _scoped_runner(event_loop_policy, request: 'FixtureRequest') -> 'Iterator[Runner]'
======================================================= short test summary info =======================================================
ERROR test_foo.py::test_foo - Failed: ScopeMismatch: You tried to access the function scoped fixture _function_scoped_runner with a session scoped request object. Requesting fixture stack:
test_foo.py:4:  def foo()
Requested fixture:
pytest-asyncio/pytest_asyncio/plugin.py:774:  def _scoped_runner(event_loop_policy, request: 'FixtureRequest') -> 'Iterator[Runner]'
========================================================== 1 error in 0.12s ===========================================================

Reproduced on 92962d18123b6314b85ae767e706481ea84a3491.

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 with the reproducer in test_foo.py and run pytest with asyncio_default_fixture_loop_scope=function. Then inspect pytest_asyncio/plugin.py around _scoped_runner at line 774 and trace how the session-scoped fixture requests its runner. Done means the reproducer no longer raises ScopeMismatch under this configuration, with regression coverage for the session fixture.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.