Skip to content

🐛 fix(config): name the TOML file that fails to parse - #333

Merged
gaborbernat merged 3 commits into
pytest-dev:mainfrom
gaborbernat:fix/toml-parse-error
Nov 17, 2026
Merged

gaborbernat merged 3 commits into
pytest-dev:mainfrom
gaborbernat:fix/toml-parse-error

Conversation

@gaborbernat

Copy link
Copy Markdown
Collaborator

_has_env_section parsed every pytest.toml, .pytest.toml and pyproject.toml that discovery handed it with an unguarded tomllib.load, and tested "pytest_env" in config without checking types (old lines). A file that did not parse, in any directory above a project, stopped every run below it with a traceback that gave a line and a column and no path, even when the project did not use pytest-env. 🐛 After this PR the error names the file, and a tool or pytest_env value that is not a table no longer counts as a section. Fixes #325.

The parse error carried no path

_find_toml_config walks from the configuration directory to the filesystem root, so it reads files pytest's own search does not reach once pytest has found the project's configuration. tomllib.TOMLDecodeError holds a position and no file name, and nothing caught it. _has_env_section now catches TOMLDecodeError and UnicodeDecodeError and raises pytest.UsageError with the path in front of the message, the format pytest uses for its own configuration files.

A non-table value was taken for a section

"pytest_env" in config is a substring test when config is a string. A pyproject.toml with tool = "notes: try pytest_env later" was selected as the configuration and failed in _load_toml_config with AttributeError: 'str' object has no attribute 'get'; tool = 1 raised TypeError. The function now reports a section only when it is a table, and for pyproject.toml only when tool is a table too. An explicit empty table still counts and still stops discovery.

Behavior changes

A TOML file on the discovery path that does not parse stops pytest with ERROR: <path>: <parser message> and exit code 4, where it used to end in a traceback. The run still stops: pytest-env does not skip a broken file. A file whose pytest_env or tool is not a table is passed over and discovery continues to the next candidate.

Reported by Anthropic's OSS Scanner: ANT-2026-ZUBFXUVY.

Discovery parses every pytest.toml, .pytest.toml and pyproject.toml between
the configuration directory and the filesystem root. A half-written file
in a parent directory stopped every run below it with a bare
TOMLDecodeError that gave a line and column and no path. A `tool` or
`pytest_env` value that is not a table was taken for a section and crashed
later with AttributeError or TypeError.

Reported by Anthropic's OSS Scanner: ANT-2026-ZUBFXUVY.

Fixes pytest-dev#325
@gaborbernat gaborbernat added the bug Something isn't working label Nov 17, 2026
@gaborbernat
gaborbernat merged commit af0bc3d into pytest-dev:main Nov 17, 2026
19 checks passed
@gaborbernat
gaborbernat deleted the fix/toml-parse-error branch November 17, 2026 23:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invalid TOML in a parent directory stops pytest without naming the file

1 participant