Visitar URL original
Read only [tool.codespell] from TOML config (#3975) · codespell-project/codespell@57b2140 · GitHub
Skip to content

Commit 57b2140

Browse files
authored
Read only [tool.codespell] from TOML config (#3975)
1 parent 23b8d94 commit 57b2140

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

‎codespell_lib/_codespell.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -712,10 +712,12 @@ def convert_arg_line_to_args(self, arg_line: str) -> list[str]:
712712
if tomllib is not None:
713713
for toml_file in toml_files:
714714
with open(toml_file, "rb") as f:
715-
data = tomllib.load(f).get("tool", {})
716-
if "codespell" in data:
717-
data["codespell"] = _toml_to_parseconfig(data["codespell"])
718-
config.read_dict(data)
715+
data = tomllib.load(f).get("tool", {}).get("codespell")
716+
if data is not None:
717+
if not isinstance(data, dict):
718+
msg = f"{toml_file}: [tool.codespell] must be a table"
719+
raise configparser.Error(msg)
720+
config.read_dict({"codespell": _toml_to_parseconfig(data)})
719721

720722
# Collect which config files are going to be used
721723
used_cfg_files = []

‎codespell_lib/tests/test_basic.py‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,10 @@ def test_ill_formed_ini_config_file(
14281428
assert "ill-formed config file" in stderr
14291429

14301430

1431-
@pytest.mark.parametrize("kind", ["cfg", "cfg_multiline", "toml", "toml_list"])
1431+
@pytest.mark.parametrize(
1432+
"kind",
1433+
["cfg", "cfg_multiline", "toml", "toml_list", "toml_sibling_array"],
1434+
)
14321435
def test_config_toml(
14331436
tmp_path: Path,
14341437
capsys: pytest.CaptureFixture[str],
@@ -1482,13 +1485,23 @@ def test_config_toml(
14821485
check-filenames = false
14831486
count = true
14841487
"""
1485-
else:
1486-
assert kind == "toml_list"
1488+
elif kind == "toml_list":
14871489
text = """\
14881490
[tool.codespell]
14891491
skip = ['bad.txt', 'whatever.txt']
14901492
check-filenames = false
14911493
count = true
1494+
"""
1495+
else:
1496+
assert kind == "toml_sibling_array"
1497+
text = """\
1498+
[tool.codespell]
1499+
skip = 'bad.txt,whatever.txt'
1500+
check-filenames = false
1501+
count = true
1502+
1503+
[[tool.dynamic-metadata]]
1504+
provider = 'scikit_build_core.metadata.version'
14921505
"""
14931506
tomlfile.write_text(text)
14941507

@@ -1514,6 +1527,22 @@ def test_config_toml(
15141527
assert "abandonned.txt" not in stdout
15151528

15161529

1530+
def test_config_toml_codespell_array(
1531+
tmp_path: Path,
1532+
capsys: pytest.CaptureFixture[str],
1533+
) -> None:
1534+
if sys.version_info < (3, 11):
1535+
pytest.importorskip("tomli")
1536+
tomlfile = tmp_path / "pyproject.toml"
1537+
tomlfile.write_text("[[tool.codespell]]\nskip = 'bad.txt'\n")
1538+
1539+
result = cs.main("--toml", tomlfile, std=True)
1540+
assert isinstance(result, tuple)
1541+
code, _, stderr = result
1542+
assert code == EX_CONFIG
1543+
assert "[tool.codespell] must be a table" in stderr
1544+
1545+
15171546
@contextlib.contextmanager
15181547
def FakeStdin(text: str) -> Generator[None, None, None]:
15191548
oldin = sys.stdin

0 commit comments

Comments
 (0)