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
19 changes: 19 additions & 0 deletions 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 Down Expand Up @@ -41,6 +44,22 @@ 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 opt.startswith("no") and f"--{opt[3:]}" in option_strings:
opposite_opt = opt[3:]
val, opp_val = ["false", "true"][value], ["true", "false"][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/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"""
24 changes: 24 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,27 @@ 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 arguments 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)