Skip to content

✨ feat(config): show clean errors for type mismatches#3838

Merged
gaborbernat merged 4 commits intotox-dev:mainfrom
gaborbernat:3831
Feb 27, 2026
Merged

✨ feat(config): show clean errors for type mismatches#3838
gaborbernat merged 4 commits intotox-dev:mainfrom
gaborbernat:3831

Conversation

@gaborbernat
Copy link
Copy Markdown
Member

@gaborbernat gaborbernat commented Feb 27, 2026

When a TOML config contains a type mismatch — for example deps = [1], or a replace = "if" whose then produces a nested list like [['pkg']] — tox previously crashed with an opaque TypeError and a full stack trace logged as "internal error." This made it difficult for users to understand what went wrong or how to fix their configuration.

The change wraps config load failures in TomlLoader.build() as HandledError with the environment name and config key in the message (e.g. failed to load py.deps: deps expected str, list[str], or list[Requirement], got list with invalid items: [0] int), mirroring how the ini loader already handles this. The _evaluate() function in the runner now catches HandledError and logs it cleanly instead of falling through to the generic "internal error" handler. Factory methods in PythonDeps, PythonConstraints, and set_env now produce descriptive error messages that identify the expected types, the actual types found, and the offending items by index.

The error handling applies to all config type mismatches, not just replacement-related ones. Any invalid value in a TOML config key will now produce a clear, actionable message without a stack trace.

Fixes #3831

Config type errors in TOML (e.g. deps = [1] or a replace-if producing
a nested list) previously crashed with an opaque TypeError and a full
stack trace logged as "internal error" during tox run.

Wrap config load failures in TomlLoader.build() as HandledError with
env/key context, catch HandledError in _evaluate() so it logs cleanly
instead of as "internal error", and improve PythonDeps/PythonConstraints
factory messages to identify which items are invalid.
@gaborbernat gaborbernat added bug:normal affects many people or has quite an impact area:configuration labels Feb 27, 2026
The factory error message changed from dumping the raw value to a
descriptive message identifying invalid items by index and type.
@gaborbernat gaborbernat added enhancement and removed bug:normal affects many people or has quite an impact labels Feb 27, 2026
@gaborbernat gaborbernat changed the title 🐛 fix(config): show clean errors for type mismatches ✨ feat(config): show clean errors for type mismatches Feb 27, 2026
Show full error details in HandledError messages instead of repr().
Add descriptive messages to all opaque TypeError raises in set_env,
MemoryLoader.to_command, and MemoryLoader.to_env_list.
@gaborbernat gaborbernat enabled auto-merge (squash) February 27, 2026 18:39
@gaborbernat gaborbernat merged commit f7ecc1f into tox-dev:main Feb 27, 2026
27 checks passed
gaborbernat added a commit to gaborbernat/tox that referenced this pull request Feb 27, 2026
The error message tests from tox-dev#3838 only checked partial messages or a
handful of fields. This makes it hard to catch regressions where the
context prefix ("failed to load <env>.<key>: ...") gets lost or where
specific field types produce unhelpful errors.

Adds a comprehensive parametrized test covering every user-configurable
field type: str, bool, int, float, Path, list[str], set[str],
list[Command], EnvList, and factory-based fields (deps, constraints).
Updates existing toml_loader and req_file tests to assert the complete
error message including the field context prefix.
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.

Passing environment variables to conditionally formatted strings seems broken

1 participant