Skip to content

Commit 4437281

Browse files
committed
Fix: Add validation for asyncio_default_fixture_loop_scope
fix linters issue added changelog entry
1 parent 4fbe1e0 commit 4437281

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

changelog.d/1189.added.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Raise a ``pytest.UsageError`` on invalid ``asyncio_default_fixture_loop_scope`` and ``asyncio_default_test_loop_scope`` configuration values. (`#1189 <https://github.com/pytest-dev/pytest-asyncio/pull/1189>`_)

pytest_asyncio/plugin.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,27 @@ def _get_asyncio_debug(config: Config) -> bool:
229229
"""
230230

231231

232+
def _validate_scope(scope: str | None, option_name: str) -> None:
233+
if scope is None:
234+
return
235+
valid_scopes = [s.value for s in Scope]
236+
if scope not in valid_scopes:
237+
raise pytest.UsageError(
238+
f"{scope!r} is not a valid {option_name}. "
239+
f"Valid scopes are: {', '.join(valid_scopes)}."
240+
)
241+
242+
232243
def pytest_configure(config: Config) -> None:
233-
default_loop_scope = config.getini("asyncio_default_fixture_loop_scope")
234-
if not default_loop_scope:
244+
default_fixture_loop_scope = config.getini("asyncio_default_fixture_loop_scope")
245+
_validate_scope(
246+
default_fixture_loop_scope, "asyncio_default_fixture_loop_scope"
247+
)
248+
if not default_fixture_loop_scope:
235249
warnings.warn(PytestDeprecationWarning(_DEFAULT_FIXTURE_LOOP_SCOPE_UNSET))
250+
251+
default_test_loop_scope = config.getini("asyncio_default_test_loop_scope")
252+
_validate_scope(default_test_loop_scope, "asyncio_default_test_loop_scope")
236253
config.addinivalue_line(
237254
"markers",
238255
"asyncio: "

tests/test_fixture_loop_scopes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,18 @@ async def test_runs_in_fixture_loop(fixture_loop):
136136
)
137137
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
138138
result.assert_outcomes(passed=1)
139+
140+
141+
def test_invalid_default_fixture_loop_scope_raises_error(pytester: Pytester):
142+
pytester.makeini(
143+
"""\
144+
[pytest]
145+
asyncio_default_fixture_loop_scope = invalid_scope
146+
"""
147+
)
148+
result = pytester.runpytest()
149+
result.stderr.fnmatch_lines([
150+
"ERROR: 'invalid_scope' is not a valid "
151+
"asyncio_default_fixture_loop_scope. Valid scopes are: "
152+
"function, class, module, package, session."
153+
])

0 commit comments

Comments
 (0)