Skip to content

Commit

Permalink
Adapt dict read from TOML config files
Browse files Browse the repository at this point in the history
Indeed, parseconfig.read_dict() expects a dict read from an INI file,
which lacks booleans.
  • Loading branch information
DimitriPapadopoulos committed Aug 27, 2023
1 parent ea9208a commit f75b445
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import re
import sys
import textwrap
from typing import Dict, List, Match, Optional, Pattern, Sequence, Set, Tuple
from typing import Any, Dict, List, Match, Optional, Pattern, Sequence, Set, Tuple

# autogenerated by setuptools_scm
from ._version import __version__ as VERSION # type: ignore # noqa: N812
Expand Down Expand Up @@ -301,6 +301,19 @@ def _split_lines(self, text: str, width: int) -> List[str]:
return out


def _toml_to_parseconfig(toml_dict: Dict[str, Any]) -> Dict[str, Any]:
"""Convert a dict read from a TOML file to the parseconfig.read_dict() format."""
parseconfig_dict = {}
for key, value in toml_dict.items():
if value is False:
pass
elif value is True:
parseconfig_dict[key] = ""

Check warning on line 311 in codespell_lib/_codespell.py

View check run for this annotation

Codecov / codecov/patch

codespell_lib/_codespell.py#L311

Added line #L311 was not covered by tests
else:
parseconfig_dict[key] = value
return parseconfig_dict


def parse_options(
args: Sequence[str],
) -> Tuple[argparse.Namespace, argparse.ArgumentParser, List[str]]:
Expand Down Expand Up @@ -566,6 +579,9 @@ def parse_options(
for toml_file in toml_files:
with open(toml_file, "rb") as f:
data = tomllib.load(f).get("tool", {})
for tool, tool_dict in data.items():
if tool == "codespell": # only "codespell" for performance
data[tool] = _toml_to_parseconfig(tool_dict)
config.read_dict(data)

# Collect which config files are going to be used
Expand Down

0 comments on commit f75b445

Please sign in to comment.