Skip to content
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ write = true
exclude = "**/my_dir/**,**/my_other_dir/**"
# Or:
exclude = ["**/my_dir/**", "**/my_other_dir/**"]
strip-whitespaces = true
split-summary-body = false
numpydoc-section-hyphen-length = false
```

#### Style
Expand Down
27 changes: 26 additions & 1 deletion pydocstringformatter/_configuration/toml_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import sys
from typing import Any

from pydocstringformatter._configuration.boolean_option_action import (
BooleanOptionalAction,
)
from pydocstringformatter._utils.exceptions import TomlParsingError, UnrecognizedOption

if sys.version_info <= (3, 11):
Expand All @@ -28,7 +31,7 @@ def get_toml_file() -> dict[str, Any] | None:
return None


def parse_toml_option(
def parse_toml_option( # pylint: disable=too-many-branches
parser: argparse.ArgumentParser, opt: str, value: Any
) -> list[str]:
"""Parse an options value in the correct argument type for argparse."""
Expand All @@ -41,6 +44,28 @@ def parse_toml_option(
except KeyError:
raise UnrecognizedOption(f"Don't recognize option {opt}") from exc

if isinstance(action, BooleanOptionalAction):
option_strings = action.option_strings

if not isinstance(value, bool):
error_msg = f"{{'{value}'}} {type(value)} is not a supported argument for"
error_msg += f"'{opt}', please use either {{true}} or {{false}}."
raise ValueError(error_msg)

if opt.startswith("no") and f"--{opt[3:]}" in option_strings:
opposite_opt = opt[3:]
val = ["false", "true"][bool(value)]
opp_val = ["true", "false"][bool(value)]
error_msg = (
"TOML file contains an unsupported option "
f"'{opt}: {val}', try using '{opposite_opt}: {opp_val}' instead"
)
raise TomlParsingError(error_msg)

index = option_strings.index(f"--{'no-' if not value else ''}{opt}")
option = action.option_strings[index]
return [option]

if isinstance(action, argparse._StoreTrueAction):
if value is True:
return [action.option_strings[0]]
Expand Down
6 changes: 6 additions & 0 deletions tests/data/config/non_valid_toml_boolopt/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[tool.pydocstringformatter]
write = false
style = ["numpydoc"]
strip-whitespaces = true
split-summary-body = false
no-numpydoc-section-hyphen-length = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def func():
"""
A docstring"""
6 changes: 6 additions & 0 deletions tests/data/config/non_valid_toml_boolopt_two/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[tool.pydocstringformatter]
write = false
style = ["numpydoc"]
strip-whitespaces = true
split-summary-body = false
numpydoc-section-hyphen-length = 'true'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def func():
"""
A docstring"""
6 changes: 6 additions & 0 deletions tests/data/config/valid_toml_boolopt/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[tool.pydocstringformatter]
write = false
style = ["numpydoc"]
strip-whitespaces = true
split-summary-body = false
numpydoc-section-hyphen-length = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def func():
"""
A docstring"""
38 changes: 38 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,41 @@ def test_style_in_toml(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(CONFIG_DATA / "valid_toml_numpydoc_pep257")
run = _Run(["test_package"])
assert run.config.style == ["numpydoc", "pep257"]

def test_boolopt_in_toml(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that arguments of type BooleanOptionalAction work in toml files."""
monkeypatch.chdir(CONFIG_DATA / "valid_toml_boolopt")
run = _Run(["test_package"])
assert not run.config.summary_quotes_same_line

# dashes in the name dont allow the dot notation to get this value
assert not run.config.__dict__["numpydoc-section-hyphen-length"]
assert run.config.__dict__["strip-whitespaces"]

def test_non_valid_boolopt_in_toml(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that '--no' versions of BooleanOptionalAction in toml do not work."""
monkeypatch.chdir(CONFIG_DATA / "non_valid_toml_boolopt")
with pytest.raises(exceptions.TomlParsingError) as err:
_Run(["test_package"])

error_msg = (
"TOML file contains an unsupported option "
"'no-numpydoc-section-hyphen-length: true', try using "
"'numpydoc-section-hyphen-length: false' instead"
)

assert error_msg in str(err.value)

def test_non_bool_boolopt_in_toml(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that non-bool values of BooleanOptionalAction in toml do not work."""
monkeypatch.chdir(CONFIG_DATA / "non_valid_toml_boolopt_two")
with pytest.raises(ValueError) as err:
_Run(["test_package"])

error_msg = (
"{'true'} <class 'str'> is not a supported argument"
" for'numpydoc-section-hyphen-length',"
" please use either {true} or {false}."
)

assert error_msg in str(err.value)