{"id":"741a02fdb389b798","repo":"pytest-dev/pytest","slug":"filepath-exc","errorCode":null,"errorMessage":"{filepath}: {exc}","messagePattern":"\\{filepath\\}: \\{exc\\}","errorType":"exception","errorClass":"UsageError","httpStatus":null,"severity":"error","filePath":"src/_pytest/config/findpaths.py","lineNumber":106,"sourceCode":"        elif \"pytest\" in iniconfig.sections:\n            # If a setup.cfg contains a \"[pytest]\" section, we raise a failure to indicate users that\n            # plain \"[pytest]\" sections in setup.cfg files is no longer supported (#3086).\n            fail(CFG_PYTEST_SECTION.format(filename=\"setup.cfg\"), pytrace=False)\n\n    # '.toml' files are considered if they contain a [tool.pytest] table (toml mode)\n    # or [tool.pytest.ini_options] table (ini mode) for pyproject.toml,\n    # or [pytest] table (toml mode) for pytest.toml/.pytest.toml.\n    elif filepath.suffix == \".toml\":\n        if sys.version_info >= (3, 11):\n            import tomllib\n        else:\n            import tomli as tomllib\n\n        toml_text = filepath.read_text(encoding=\"utf-8\")\n        try:\n            config = tomllib.loads(toml_text)\n        except tomllib.TOMLDecodeError as exc:\n            raise UsageError(f\"{filepath}: {exc}\") from exc\n\n        # pytest.toml and .pytest.toml use [pytest] table directly.\n        if filepath.name in (\"pytest.toml\", \".pytest.toml\"):\n            if \"pytest\" in config:\n                # TOML mode - preserve native TOML types.\n                return {\n                    k: ConfigValue(v, origin=\"file\", mode=\"toml\")\n                    for k, v in config[\"pytest\"].items()\n                }\n            top_level_options = [\n                key for key, value in config.items() if not isinstance(value, dict)\n            ]\n            if top_level_options:\n                raise UsageError(\n                    f\"{filepath}: pytest configuration must be under a \"\n                    f\"[pytest] table (found top-level options: \"\n                    f\"{', '.join(top_level_options)})\"\n                )","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/pytest-dev/pytest/blob/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/config/findpaths.py#L88-L124","documentation":"Raised when pytest's config loader fails to parse a TOML configuration file (pytest.toml, .pytest.toml, or the [tool.pytest] section of pyproject.toml) due to malformed TOML syntax. The stdlib tomllib (or tomli on older Python) raises TOMLDecodeError, which pytest wraps as a UsageError prefixed with the file path.","triggerScenarios":"A pyproject.toml or pytest.toml with syntax errors: unquoted strings, mismatched brackets, trailing characters, invalid escape sequences, or duplicate keys. tomllib.loads() raises TOMLDecodeError at line 104-106.","commonSituations":"Hand-editing pyproject.toml and introducing a syntax error; merging tool sections incorrectly; copying TOML from a source that used a different format; using TOML features unsupported by the bundled parser version.","solutions":["Validate the TOML file: python -c 'import tomllib; tomllib.loads(open('pyproject.toml').read())'.","Use a TOML linter or editor with TOML syntax checking.","Fix the specific syntax error described in the wrapped TOMLDecodeError message (line/column shown)."],"exampleFix":"# before (unquoted value)\n[tool.pytest.ini_options]\naddopts = -v\n\n# after\n[tool.pytest.ini_options]\naddopts = '-v'","handlingStrategy":"try-catch","validationCode":"import sys, pathlib\ntry:\n    import tomllib\nexcept ImportError:\n    import tomli as tomllib\n\ndef validate_toml(path: pathlib.Path) -> None:\n    try:\n        tomllib.loads(path.read_text(encoding='utf-8'))\n    except tomllib.TOMLDecodeError as e:\n        raise ValueError(f'{path}: {e}') from e","typeGuard":null,"tryCatchPattern":"try:\n    config = tomllib.loads(text)\nexcept tomllib.TOMLDecodeError as exc:\n    raise ValueError(f'{filepath}: {exc}') from exc","preventionTips":["Run a TOML parse check on pyproject.toml in pre-commit / CI.","Use an editor with TOML syntax highlighting and validation.","Quote string values and avoid mixing array/inline-table syntax carelessly."],"tags":["pytest","config","toml","pyproject","parsing","usage-error"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}