From 418f3528bb504809b83e807cb30d3b067e0dcfd9 Mon Sep 17 00:00:00 2001 From: Andreas Poehlmann Date: Sun, 31 Mar 2024 19:43:06 +0200 Subject: [PATCH] Fix pyproject.toml tool.mypy.overrides parsing (#146) --- pytest_mypy_plugins/configs.py | 11 ++++++++-- .../tests/test_configs/pyproject3.toml | 11 ++++++++++ .../test_configs/test_join_toml_configs.py | 20 +++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 pytest_mypy_plugins/tests/test_configs/pyproject3.toml diff --git a/pytest_mypy_plugins/configs.py b/pytest_mypy_plugins/configs.py index 7595500..6adbf33 100644 --- a/pytest_mypy_plugins/configs.py +++ b/pytest_mypy_plugins/configs.py @@ -54,6 +54,13 @@ def join_toml_configs( with mypy_config_file_path.open("w") as f: # We don't want the whole config file, because it can contain # other sections like `[tool.isort]`, we only need `[tool.mypy]` part. - f.write(f"{_TOML_TABLE_NAME}\n") - f.write(dedent(toml_config["tool"]["mypy"].as_string())) # type: ignore[index] + tool_mypy = toml_config["tool"]["mypy"] # type: ignore[index] + + # construct toml output + min_toml = tomlkit.document() + min_tool = tomlkit.table(is_super_table=True) + min_toml.append("tool", min_tool) + min_tool.append("mypy", tool_mypy) + + f.write(min_toml.as_string()) return str(mypy_config_file_path) diff --git a/pytest_mypy_plugins/tests/test_configs/pyproject3.toml b/pytest_mypy_plugins/tests/test_configs/pyproject3.toml new file mode 100644 index 0000000..4e87f17 --- /dev/null +++ b/pytest_mypy_plugins/tests/test_configs/pyproject3.toml @@ -0,0 +1,11 @@ +# This file has `[tool.mypy]` existing config + +[tool.mypy] +warn_unused_ignores = true +pretty = true +show_error_codes = true + +[[tool.mypy.overrides]] +# This section should be copied +module = "mymodule" +ignore_missing_imports = true diff --git a/pytest_mypy_plugins/tests/test_configs/test_join_toml_configs.py b/pytest_mypy_plugins/tests/test_configs/test_join_toml_configs.py index d1f2ab4..d9d3e75 100644 --- a/pytest_mypy_plugins/tests/test_configs/test_join_toml_configs.py +++ b/pytest_mypy_plugins/tests/test_configs/test_join_toml_configs.py @@ -21,6 +21,7 @@ _PYPROJECT1: Final = str(Path(__file__).parent / "pyproject1.toml") _PYPROJECT2: Final = str(Path(__file__).parent / "pyproject2.toml") +_PYPROJECT3: Final = str(Path(__file__).parent / "pyproject3.toml") @pytest.fixture @@ -112,3 +113,22 @@ def test_join_missing_config2(execution_path: Path, assert_file_contents: _Asser filepath, "[tool.mypy]", ) + + +def test_join_missing_config3(execution_path: Path, assert_file_contents: _AssertFileContents) -> None: + filepath = join_toml_configs(_PYPROJECT3, "", execution_path) + + assert_file_contents( + filepath, + """ + [tool.mypy] + warn_unused_ignores = true + pretty = true + show_error_codes = true + + [[tool.mypy.overrides]] + # This section should be copied + module = "mymodule" + ignore_missing_imports = true + """, + )