Skip to content

Commit

Permalink
Fix pyproject.toml tool.mypy.overrides parsing (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
ap-- committed Mar 31, 2024
1 parent a92e6bc commit 418f352
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pytest_mypy_plugins/configs.py
Expand Up @@ -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)
11 changes: 11 additions & 0 deletions 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
20 changes: 20 additions & 0 deletions pytest_mypy_plugins/tests/test_configs/test_join_toml_configs.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
""",
)

0 comments on commit 418f352

Please sign in to comment.