Skip to content

Validate asyncio marker loop_scope via _validate_scope - #1585

Open
JingHao-Leon wants to merge 1 commit into
pytest-dev:mainfrom
JingHao-Leon:fix-marker-loop-scope-validation
Open

JingHao-Leon wants to merge 1 commit into
pytest-dev:mainfrom
JingHao-Leon:fix-marker-loop-scope-validation

Conversation

@JingHao-Leon

@JingHao-Leon JingHao-Leon commented Oct 19, 2026 •

Copy link
Copy Markdown

Description

An invalid loop_scope on the asyncio marker is validated with a bare assert, so a typo
is reported as a Python AssertionError printing a set repr:

@pytest.mark.asyncio(loop_scope="invalid_scope")
async def test_x():
    pass
E   AssertionError: assert 'invalid_scope' in {'class', 'function', 'module', 'package', 'session'}
ERROR test_bad.py - AssertionError: assert 'invalid_scope' in {'class', 'function', ...

The same mistake in asyncio_default_test_loop_scope already reads
ERROR: "bogus" is not a valid asyncio_default_test_loop_scope. Valid scopes are: function, class, module, package, session.

Both settings accept the same five scopes, and the plugin already has a helper that produces the
second message — the marker path just doesn't call it. The reported error also doesn't say which
argument was wrong.

This changes the reported message only; it does not change which inputs are accepted or rejected.

Root cause

pytest_asyncio/plugin.py:982-983, in _parse_asyncio_marker:

    if scope is not None:
        assert scope in {"function", "class", "module", "package", "session"}

This is the only validation of the marker's loop_scope/scope. pytest_configure validates
the two ini options through _validate_scope (pytest_asyncio/plugin.py:285), which raises
pytest.UsageError with the message above and returns early for None. Replacing the assert
with that call makes both paths use the same validation:

    _validate_scope(scope, "loop_scope")

Validation

tests/test_fixture_loop_scopes.py::test_invalid_marker_loop_scope_raises_error, added next to
the existing test_invalid_default_fixture_loop_scope_raises_error, which covers the ini-option
half of the same contract.

# before the fix
PYTHONPATH=$PWD python -m pytest tests/test_fixture_loop_scopes.py::test_invalid_marker_loop_scope_raises_error -q
1 failed          # reported: AssertionError: assert 'invalid_scope' in {...}

# after the fix
1 passed          # reported: pytest.UsageError: 'invalid_scope' is not a valid loop_scope.
                  #   Valid scopes are: function, class, module, package, session.

# full suite
PYTHONPATH=$PWD python -m pytest -q
296 passed, 3 skipped

Clean main baseline for comparison is 295 passed, 3 skipped; the one added test accounts for
the difference. No existing test changed behaviour.

Python 3.13.9, pytest 9.1.1. PYTHONPATH=$PWD is set because this machine also has
pytest-asyncio installed in site-packages; python -c "import pytest_asyncio; print(pytest_asyncio.__file__)"
resolves to the checkout under that path.

Fixes #1584

This change was prepared with the assistance of an AI coding agent.

The marker's loop_scope was checked with a bare assert, so an invalid value
surfaced as AssertionError: assert 'x' in {'class', 'function', ...}. Use the
helper that already validates the two ini options instead, so the reported
message names the argument and lists the valid scopes.
@JingHao-Leon
JingHao-Leon force-pushed the fix-marker-loop-scope-validation branch from 568df45 to 9aa2fa5 Compare October 19, 2026 10:33
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.89%. Comparing base (93893ec) to head (9aa2fa5).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1585      +/-   ##
==========================================
+ Coverage   94.50%   94.89%   +0.38%     
==========================================
  Files           2        2              
  Lines         510      509       -1     
  Branches       62       61       -1     
==========================================
+ Hits          482      483       +1     
+ Misses         22       21       -1     
+ Partials        6        5       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invalid loop_scope in the asyncio marker raises AssertionError instead of a usage error

2 participants