Skip to content

Report a bad ini core value as a handled error - #4028

Open
VXNCXNX wants to merge 2 commits into
tox-dev:mainfrom
VXNCXNX:fix/ini-core-value-handled-error
Open

Report a bad ini core value as a handled error#4028
VXNCXNX wants to merge 2 commits into
tox-dev:mainfrom
VXNCXNX:fix/ini-core-value-handled-error

Conversation

@VXNCXNX

@VXNCXNX VXNCXNX commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

A bad value in the ini [tox] core section exits with a raw traceback, while the same value in pyproject.toml is already reported properly.

$ printf '[tox]\nmin_version = notaversion\n' > tox.ini && tox l
  File ".../packaging/version.py", line 452, in __init__
    raise InvalidVersion(f"Invalid version: {version!r}")
packaging.version.InvalidVersion: Invalid version: 'notaversion'

The TOML path, same input, unchanged by this PR:

$ printf '[tool.tox]\nmin_version = "notaversion"\n' > pyproject.toml && tox l
ROOT: HandledError| failed to load core.min_version: Invalid version: 'notaversion'

After:

ROOT: HandledError| failed to load tox.min_version: Invalid version: 'notaversion'

requires = ===bad!!! and env_list = {py39,py310 (unbalanced brace) were tracebacks too, and are now handled the same way.

The fix

IniLoader.build called self.to(...) unguarded. TomlLoader.build already wraps the identical call and re-raises as HandledError, so this mirrors it, including letting HandledError and Skip through untouched.

Why the guard is narrow

The wrap only applies to the core section. My first attempt covered every section and broke 6 existing tests, because two paths deliberately let raw exceptions through:

  • testenv values, where tox c renders the exception inline as # Exception: ... (test_config_bad_dict, test_config_bad_bool and friends)
  • the CLI config file, loaded with conf is None

Those are existing behaviour I did not want to change, so the condition is conf is None or args.env_name is not None to bypass. Core values are the case with no handling at all, because they are loaded before any guard is in place.

Verification

test_ini_core_bad_value_is_handled_error in tests/config/source/test_discover.py, parametrised over min_version, requires and env_list. It asserts no unhandled exception leaks, so it fails on the actual symptom rather than on a message string.

Reverting only the guard condition to always bypass fails it as an assertion:

>       assert leaked is None, f"unhandled {type(leaked).__name__}: {leaked}"
E       AssertionError: unhandled InvalidVersion: Invalid version: 'notaversion'
E       AssertionError: unhandled InvalidRequirement: Expected package name at the start of dependency specifier
E       AssertionError: unhandled ValueError: {py39

pytest tests/config/ is 6644 passed, 2 skipped. tests/config/cli/test_argcomplete.py is excluded because argcomplete is not installed here; it fails to collect identically on a clean checkout.

Disclosure: written with AI assistance (Claude Code). I reproduced the traceback and the TOML contrast at the CLI, confirmed the narrow guard is required by removing it and watching 6 tests break, and ran the mutation check myself.

The ini loader called self.to() unguarded, so a conversion failure on a
[tox] core value escaped as a raw traceback. The TOML loader already
wraps the same call and raises HandledError, so identical input gave a
clean message in pyproject.toml but a traceback in tox.ini.

@gaborbernat gaborbernat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge conconflict 🤔

@MohammedAlkindi

Copy link
Copy Markdown
Contributor

Reproduced this on Windows 11 / Python 3.14.7 with printf '[tox]\nmin_version = notaversion\n' > tox.ini, then tox -c <that file> l.

On main at 0250664d:

  File ".../packaging/version.py", line 452, in __init__
    raise InvalidVersion(f"Invalid version: {version!r}")
packaging.version.InvalidVersion: Invalid version: 'notaversion'

On this branch at 4616771c:

ROOT: HandledError| failed to load tox.min_version: Invalid version: 'notaversion'

tests/config/source/test_discover.py gives 11 passed.

Small observation rather than a request: the ini path reports tox.min_version while the equivalent pyproject.toml case reports core.min_version. Both are handled errors so nothing is broken, but the key differs between the two sources.

Ran the commands above myself; the write-up was drafted with AI assistance.

@CAOShurong CAOShurong left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified end-to-end on the current head (4616771), Windows / Python 3.13, tox installed from the exact head SHA into an isolated --target dir:

  • baseline + in tox.ini: raw traceback (unhandled), exit 1 — reproduced;
  • this PR head, same ini: , no traceback, and the same exit code and message shape as the existing TOML path ( on both main and this head, unchanged), so the "matching the TOML loader behavior" claim holds exactly;
  • the HandledError/Skip passthrough with broad wrap only for core-section values (env values keep their own guards) is the right scope.

One cosmetic note, not a blocker: the ini path prefixes the key with while the TOML path uses — inherent to each loader's section name, just worth knowing when grepping logs.

@CAOShurong CAOShurong left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified end-to-end on the current head (4616771), Windows / Python 3.13, tox installed from the exact head SHA into an isolated --target dir:

  • baseline tox @ main with min_version = notaversion in tox.ini: raw packaging.version.InvalidVersion traceback (unhandled), exit 1 — reproduced;
  • this PR head, same ini: "ROOT: HandledError| failed to load tox.min_version: Invalid version: 'notaversion'" — no traceback, and the same exit code and message shape as the existing TOML path ("failed to load core.min_version: ...", identical on main and this head), so the "matching the TOML loader behavior" claim holds exactly;
  • the HandledError/Skip passthrough with the broad Exception wrap applied only to core-section values (environment values keep their own guards) is the right scope.

One cosmetic note, not a blocker: the ini path prefixes the key with "tox." while the TOML path uses "core." — inherent to each loader's section name, just worth knowing when grepping logs.

@gaborbernat gaborbernat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has a merge conflict.

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.

4 participants