Skip to content

Commit 418f352

Browse files
authored
Fix pyproject.toml tool.mypy.overrides parsing (#146)
1 parent a92e6bc commit 418f352

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

pytest_mypy_plugins/configs.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ def join_toml_configs(
5454
with mypy_config_file_path.open("w") as f:
5555
# We don't want the whole config file, because it can contain
5656
# other sections like `[tool.isort]`, we only need `[tool.mypy]` part.
57-
f.write(f"{_TOML_TABLE_NAME}\n")
58-
f.write(dedent(toml_config["tool"]["mypy"].as_string())) # type: ignore[index]
57+
tool_mypy = toml_config["tool"]["mypy"] # type: ignore[index]
58+
59+
# construct toml output
60+
min_toml = tomlkit.document()
61+
min_tool = tomlkit.table(is_super_table=True)
62+
min_toml.append("tool", min_tool)
63+
min_tool.append("mypy", tool_mypy)
64+
65+
f.write(min_toml.as_string())
5966
return str(mypy_config_file_path)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This file has `[tool.mypy]` existing config
2+
3+
[tool.mypy]
4+
warn_unused_ignores = true
5+
pretty = true
6+
show_error_codes = true
7+
8+
[[tool.mypy.overrides]]
9+
# This section should be copied
10+
module = "mymodule"
11+
ignore_missing_imports = true

pytest_mypy_plugins/tests/test_configs/test_join_toml_configs.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
_PYPROJECT1: Final = str(Path(__file__).parent / "pyproject1.toml")
2323
_PYPROJECT2: Final = str(Path(__file__).parent / "pyproject2.toml")
24+
_PYPROJECT3: Final = str(Path(__file__).parent / "pyproject3.toml")
2425

2526

2627
@pytest.fixture
@@ -112,3 +113,22 @@ def test_join_missing_config2(execution_path: Path, assert_file_contents: _Asser
112113
filepath,
113114
"[tool.mypy]",
114115
)
116+
117+
118+
def test_join_missing_config3(execution_path: Path, assert_file_contents: _AssertFileContents) -> None:
119+
filepath = join_toml_configs(_PYPROJECT3, "", execution_path)
120+
121+
assert_file_contents(
122+
filepath,
123+
"""
124+
[tool.mypy]
125+
warn_unused_ignores = true
126+
pretty = true
127+
show_error_codes = true
128+
129+
[[tool.mypy.overrides]]
130+
# This section should be copied
131+
module = "mymodule"
132+
ignore_missing_imports = true
133+
""",
134+
)

0 commit comments

Comments
 (0)