From 30c91b1ce4bdf8a5009675c10e0d21f05b699e49 Mon Sep 17 00:00:00 2001 From: Muhamed Fazal PS Date: Thu, 16 Jul 2026 19:10:35 +0530 Subject: [PATCH] Fix #14705: Custom TOML config files should use [pytest] section When a custom TOML configuration file is passed via --config-file, the code incorrectly looked for [tool.pytest] section (as used in pyproject.toml). Custom config files should use [pytest] section, matching the behavior of pytest.toml and documented expectations. The else branch was changed to elif filepath.name == 'pyproject.toml' so only pyproject.toml uses [tool.pytest]. A new else branch handles any other custom .toml files by looking for [pytest] table. Fixes #14705 --- src/_pytest/config/findpaths.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/_pytest/config/findpaths.py b/src/_pytest/config/findpaths.py index 2a4bed319a9..84e95b0b76d 100644 --- a/src/_pytest/config/findpaths.py +++ b/src/_pytest/config/findpaths.py @@ -92,7 +92,8 @@ def load_config_dict_from_file( # '.toml' files are considered if they contain a [tool.pytest] table (toml mode) # or [tool.pytest.ini_options] table (ini mode) for pyproject.toml, - # or [pytest] table (toml mode) for pytest.toml/.pytest.toml. + # or [pytest] table (toml mode) for pytest.toml/.pytest.toml and other custom + # TOML config files. elif filepath.suffix == ".toml": if sys.version_info >= (3, 11): import tomllib @@ -126,7 +127,7 @@ def load_config_dict_from_file( return {} # pyproject.toml uses [tool.pytest] or [tool.pytest.ini_options]. - else: + elif filepath.name == "pyproject.toml": tool_pytest = config.get("tool", {}).get("pytest", {}) # Check for toml mode config: [tool.pytest] with content outside of ini_options. @@ -160,6 +161,17 @@ def make_scalar(v: object) -> str | list[str]: for k, v in ini_config.items() } + # Any other .toml file (custom config passed via --config-file) uses [pytest] table. + else: + if "pytest" in config: + # TOML mode - preserve native TOML types. + return { + k: ConfigValue(v, origin="file", mode="toml") + for k, v in config["pytest"].items() + } + # Custom TOML files are always the source of configuration, even if empty. + return {} + return None