From 040939c1801144df5b006a0746870c3b848b2c27 Mon Sep 17 00:00:00 2001 From: Md Mushfiqur Rahim <20mahin2020@gmail.com> Date: Wed, 24 Jun 2026 01:37:09 +0000 Subject: [PATCH 1/2] fix: treat top-level keys in pytest.toml as pytest config (#14638) --- src/_pytest/config/findpaths.py | 9 ++++++++- testing/test_config.py | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/_pytest/config/findpaths.py b/src/_pytest/config/findpaths.py index e74546c6e28..987cf062e5f 100644 --- a/src/_pytest/config/findpaths.py +++ b/src/_pytest/config/findpaths.py @@ -106,8 +106,15 @@ def load_config_dict_from_file( raise UsageError(f"{filepath}: {exc}") from exc # pytest.toml and .pytest.toml use [pytest] table directly. + # Top-level non-table keys are also treated as pytest config, + # since the filename already identifies the context and TOML + # sections are optional. if filepath.name in ("pytest.toml", ".pytest.toml"): - pytest_config = config.get("pytest", {}) + top_config = { + k: v for k, v in config.items() + if not isinstance(v, dict) + } + pytest_config = {**top_config, **config.get("pytest", {})} if pytest_config: # TOML mode - preserve native TOML types. return { diff --git a/testing/test_config.py b/testing/test_config.py index 7886610242d..22f3ee6540d 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -217,6 +217,20 @@ def test_empty_pytest_toml(self, pytester: Pytester, name: str) -> None: config = pytester.parseconfig() assert config.inipath == pytest_toml + @pytest.mark.parametrize("name", ["pytest.toml", ".pytest.toml"]) + def test_headerless_pytest_toml(self, pytester: Pytester, name: str) -> None: + """Top-level keys in pytest.toml are treated as pytest config (#14638).""" + pytester.path.joinpath(name).write_text( + textwrap.dedent( + """ + minversion = "9.0" + """ + ), + encoding="utf-8", + ) + config = pytester.parseconfig() + assert config.getini("minversion") == "9.0" + def test_pytest_toml_trumps_pyproject_toml(self, pytester: Pytester) -> None: """A pytest.toml always takes precedence over a pyproject.toml file.""" pytester.makepyprojecttoml( From 3747b47a3c9197166a26e54a4033893f762f0489 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 24 Jun 2026 01:37:48 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/_pytest/config/findpaths.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/_pytest/config/findpaths.py b/src/_pytest/config/findpaths.py index 987cf062e5f..9677cc65a06 100644 --- a/src/_pytest/config/findpaths.py +++ b/src/_pytest/config/findpaths.py @@ -110,10 +110,7 @@ def load_config_dict_from_file( # since the filename already identifies the context and TOML # sections are optional. if filepath.name in ("pytest.toml", ".pytest.toml"): - top_config = { - k: v for k, v in config.items() - if not isinstance(v, dict) - } + top_config = {k: v for k, v in config.items() if not isinstance(v, dict)} pytest_config = {**top_config, **config.get("pytest", {})} if pytest_config: # TOML mode - preserve native TOML types.