Skip to content

Validate asyncio_mode at configuration time, not only in the report header - #1632

Closed
qianeric-backup wants to merge 1 commit into
pytest-dev:mainfrom
qianeric-backup:fix/validate-asyncio-mode-at-config-time
Closed

qianeric-backup wants to merge 1 commit into
pytest-dev:mainfrom
qianeric-backup:fix/validate-asyncio-mode-at-config-time

Conversation

@qianeric-backup

Copy link
Copy Markdown

_get_asyncio_mode() is called from pytest_report_header(), so the value is only checked when the header is printed. With -q the header is skipped and a typo in the option is not a configuration error any more — it is raised while setting up each test, so the run shows a traceback and a per-test ERROR instead of one message:

$ pytest -q -o asyncio_mode=bogus
...
==================================== ERRORS ====================================
___________________________ ERROR at setup of test_a ___________________________

    def _get_asyncio_mode(config: Config) -> Mode:
        val = config.getoption("asyncio_mode")
        if val is None:
            val = config.getini("asyncio_mode")
        try:
>           return Mode(val)
E       pytest.UsageError: 'bogus' is not a valid asyncio_mode. Valid modes: auto, strict.

The sibling options are validated in pytest_configure() and fail before collection:

$ pytest -q -o asyncio_default_fixture_loop_scope=bogus ; echo $?
ERROR: 'bogus' is not a valid asyncio_default_fixture_loop_scope. Valid scopes are: ...
4

Change

pytest_configure() validates the mode with the same helper the report header uses:

$ pytest -q -o asyncio_mode=bogus ; echo $?
ERROR: 'bogus' is not a valid asyncio_mode. Valid modes: auto, strict.
4
case before after
-o asyncio_mode=bogus (header printed) ERROR: ..., exit 4 unchanged
-q -o asyncio_mode=bogus per-test ERROR at setup with a traceback, exit 1 single ERROR: 'bogus' is not a valid asyncio_mode. Valid modes: auto, strict., exit 4
-o asyncio_mode=auto / strict works unchanged
default (no asyncio_mode set) Mode.STRICT unchanged

Tests

test_invalid_asyncio_mode_is_a_config_error parametrizes the existing test_invalid_asyncio_mode with and without -q and asserts the exit code is USAGE_ERROR. pytest tests/test_simple.py tests/modes tests/markers: 70 passed, 3 skipped with the change; on the current tree the -q case fails because the error is raised per test instead.

I did not add a changelog.d entry — tell me the fragment number you want and I will add it.

…eader

`_get_asyncio_mode()` is called from `pytest_report_header()`, so the value is
only checked when the header is printed. With `-q` the header is skipped and a
typo in the option is not a configuration error any more - it is raised while
setting up each test, so the run shows an internal traceback and a per-test
ERROR instead of one message:

    $ pytest -q -o asyncio_mode=bogus
    ...
    ==================================== ERRORS ====================================
    ___________________________ ERROR at setup of test_a ___________________________
        def _get_asyncio_mode(config: Config) -> Mode:
            try:
    >           return Mode(val)
    E       pytest.UsageError: 'bogus' is not a valid asyncio_mode. Valid modes: auto, strict.

The sibling options are validated in `pytest_configure()` and fail before
collection:

    $ pytest -q -o asyncio_default_fixture_loop_scope=bogus ; echo $?
    ERROR: 'bogus' is not a valid asyncio_default_fixture_loop_scope. Valid scopes are: ...
    4

## Change

`pytest_configure()` validates the mode with the same helper the report header
uses:

    $ pytest -q -o asyncio_mode=bogus ; echo $?
    ERROR: 'bogus' is not a valid asyncio_mode. Valid modes: auto, strict.
    4

| case | before | after |
|---|---|---|
| `-o asyncio_mode=bogus` (header printed) | `ERROR: ...`, exit 4 | unchanged |
| `-q -o asyncio_mode=bogus` | per-test `ERROR at setup` with a traceback, exit 1 | `ERROR: 'bogus' is not a valid asyncio_mode. Valid modes: auto, strict.`, exit 4 |
| `-o asyncio_mode=auto` / `strict` | works | unchanged |

## Tests

`test_invalid_asyncio_mode_is_a_config_error` parametrizes the existing
`test_invalid_asyncio_mode` with and without `-q` and asserts the exit code is
`USAGE_ERROR`. `pytest tests/test_simple.py tests/modes tests/markers`: 70
passed, 3 skipped with the change; on the current tree the `-q` case fails
because the error is raised per test instead.

I did not add a `changelog.d` entry - tell me the fragment number you want and I
will add it.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.87%. Comparing base (599fb32) to head (8376601).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1632      +/-   ##
==========================================
+ Coverage   94.86%   94.87%   +0.01%     
==========================================
  Files           2        2              
  Lines         506      507       +1     
  Branches       60       60              
==========================================
+ Hits          480      481       +1     
  Misses         21       21              
  Partials        5        5              

☔ 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.

Copy link
Copy Markdown
Member

🤖 Written by Claude Opus 5.5 via Claude Code for the pytest-asyncio maintainers; I prompted it, it did the work, I read it.

This PR comes from an unattended agent loop. The account has opened 93 PRs across unrelated projects since July, 30 of them on 2026-11-22 alone at 10-15 minute intervals, with no issue or discussion before any of them. Four landed in pytest-dev within a day. The sibling pytest-dev/pytest-xdist#1433 would have broken a documented configuration, because the agent misread the very docs it cited as evidence.

The one-line change here is fine on its own: with -q an invalid asyncio_mode goes unreported on a sync-only suite, and validating it in pytest_configure matches the sibling options. That does not make the stream acceptable. Maintainers cannot tell which of these PRs is the sound one and which is the one that breaks users without re-checking each of them, and the body closes by asking the maintainers to hand it a changelog fragment number, so even the follow-up is left to them.

The account is being banned from pytest-dev. Our AI policy: https://github.com/pytest-dev/pytest/blob/main/CONTRIBUTING.rst


Generated by Claude Code

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants