Skip to content

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

Closed

Description

@gaborbernat

With pytest-env installed, a pyproject.toml, pytest.toml or .pytest.toml that does not parse, in any directory above a project, stops the run before collection with a traceback that gives a line and column and no path. The project does not have to use pytest-env, and the same tree passes with -p no:env.

Affected code

Mechanism

_find_toml_config passes every candidate file between the configuration directory and / to _has_env_section, which calls tomllib.load with no handler. A half-written file raises TOMLDecodeError, whose message holds a position and not a file name. The function also tests "pytest_env" in config without checking that tool is a table, so a pyproject.toml with tool = "notes: try pytest_env later" is selected as the configuration and fails later with AttributeError: 'str' object has no attribute 'get'. pytest's own search stops at the project's configuration file and does not read the parent.

Reproduction

repro.sh

tmp="$(mktemp -d)"
mkdir "$tmp/project"
printf '[project]\nname = "scratch"\nversion =\n' > "$tmp/pyproject.toml"
cd "$tmp/project"
printf '[pytest]\n' > pytest.ini
printf 'def test_pass() -> None:\n    pass\n' > test_pass.py
pytest -q 2>&1 | tail -n 4
pytest -q -p no:env

Run with sh repro.sh at 3ebb6b4872fd0e450b6670d529e375e11c218e61, with pytest 9.2.0, python-dotenv 1.2.4 and CPython 3.14 on Linux:

                 ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.14/tomllib/_parser.py", line 740, in parse_value
    raise TOMLDecodeError("Invalid value", src, pos)
tomllib.TOMLDecodeError: Invalid value (at line 3, column 10)
.                                                                        [100%]
1 passed in 0.00s

Expected: the error names the file, as pytest's does for its own configuration files.

Impact

Every pytest run below the broken file fails until someone finds it, with no path in the output to look at.

  • Reproducibility: easy. One command once the file exists.
  • Real-world exposure: niche. It needs a project with its own configuration file, no nearer pytest_env table, and a broken TOML file in an ancestor, such as a scratch pyproject.toml in a home directory.

Credits

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

Activity

  1. added
    bug
    on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions