From 011c2afe4afe2d463340b6f7a3040b754b159b92 Mon Sep 17 00:00:00 2001 From: EnesKartal Date: Tue, 24 Nov 2026 11:07:49 +0300 Subject: [PATCH] fix: only warn about unset asyncio_default_fixture_loop_scope when pytest-asyncio is used --- pytest_asyncio/plugin.py | 16 ++++++++++++++-- tests/test_simple.py | 26 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index 32b1a834..677725e7 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -280,14 +280,24 @@ def _validate_scope(scope: str | None, option_name: str) -> None: ) +_warned_default_fixture_loop_scope_unset_key = pytest.StashKey[bool]() + + +def _warn_if_default_fixture_loop_scope_unset(config: Config) -> None: + if ( + not config.getini("asyncio_default_fixture_loop_scope") + and not config.stash.get(_warned_default_fixture_loop_scope_unset_key, False) + ): + config.stash[_warned_default_fixture_loop_scope_unset_key] = True + warnings.warn(PytestDeprecationWarning(_DEFAULT_FIXTURE_LOOP_SCOPE_UNSET)) + + def pytest_configure(config: Config) -> None: default_fixture_loop_scope = config.getini("asyncio_default_fixture_loop_scope") if default_fixture_loop_scope != _MATCH_FIXTURE_SCOPE: _validate_scope( default_fixture_loop_scope, "asyncio_default_fixture_loop_scope" ) - if not default_fixture_loop_scope: - warnings.warn(PytestDeprecationWarning(_DEFAULT_FIXTURE_LOOP_SCOPE_UNSET)) default_test_loop_scope = config.getini("asyncio_default_test_loop_scope") _validate_scope(default_test_loop_scope, "asyncio_default_test_loop_scope") @@ -861,6 +871,7 @@ def _set_event_loop(loop: AbstractEventLoop | None) -> None: def pytest_pyfunc_call(pyfuncitem: Function) -> object | None: """Pytest hook called before a test case is run.""" if pyfuncitem.get_closest_marker("asyncio") is not None: + _warn_if_default_fixture_loop_scope_unset(pyfuncitem.config) if is_async_test(pyfuncitem): asyncio_mode = _get_asyncio_mode(pyfuncitem.config) for fixname, fixtures in pyfuncitem._fixtureinfo.name2fixturedefs.items(): @@ -921,6 +932,7 @@ def pytest_fixture_setup(fixturedef: FixtureDef, request) -> object | None: return (yield) if not _is_coroutine_or_asyncgen(fixturedef.func): return (yield) + _warn_if_default_fixture_loop_scope_unset(request.config) default_loop_scope = request.config.getini("asyncio_default_fixture_loop_scope") if default_loop_scope == _MATCH_FIXTURE_SCOPE: default_loop_scope = fixturedef.scope diff --git a/tests/test_simple.py b/tests/test_simple.py index 8ddeade3..9b90f80c 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -99,3 +99,29 @@ def test_invalid_asyncio_mode(pytester): result.stderr.fnmatch_lines( "ERROR: 'True' is not a valid asyncio_mode. Valid modes: auto, strict." ) + + +def test_unset_default_fixture_loop_scope_no_warning_for_sync_suite(pytester): + pytester.makeini("[pytest]\n") + pytester.makepyfile("def test_sync(): pass") + result = pytester.runpytest_inprocess("-Werror") + assert result.ret == pytest.ExitCode.OK + result.assert_outcomes(passed=1) + + +def test_unset_default_fixture_loop_scope_warns_for_async_suite(pytester): + pytester.makeini("[pytest]\n") + pytester.makepyfile( + """ + import pytest + + @pytest.mark.asyncio + async def test_async(): + pass + """ + ) + result = pytester.runpytest_inprocess("-Werror") + assert result.ret != pytest.ExitCode.OK + result.stdout.fnmatch_lines( + "*asyncio_default_fixture_loop_scope*is unset*" + )