Skip to content

Commit fa511dc

Browse files
Add automatically generated options
1 parent e237d03 commit fa511dc

File tree

5 files changed

+71
-6
lines changed

5 files changed

+71
-6
lines changed

.github/CONTRIBUTING.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ Use `pre-commit install` to install the pre-commit hook for the repository.
88

99
- Implement a Formatter by inheriting from `pydocstringformatter.formatting.Formatter`
1010
- Add your new formatter to `pydocstringformatter.formatting.FORMATTERS`
11-
- Choose a proper name because this will be user-facing: the name will be used for
12-
options of the CLI.
11+
- Write a clear docstring because this will be user-facing: it's what will be seen in
12+
the help message for the formatters command line option.
13+
- Choose a proper name because this will be user-facing: the name will be used to turn
14+
the checker on and off via the command line or config files.
1315

1416
### Testing
1517

pydocstringformatter/run.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
from typing import List, Union
1010

1111
from pydocstringformatter import __version__, formatting, utils
12+
from pydocstringformatter.formatting import FORMATTERS
1213

1314

1415
class _Run:
1516
"""Main class that represent a run of the program."""
1617

1718
def __init__(self, argv: Union[List[str], None]) -> None:
1819
self.arg_parser = utils._register_arguments(__version__)
20+
utils._register_arguments_formatters(self.arg_parser, FORMATTERS)
1921
self.config = argparse.Namespace()
2022

2123
if argv := argv or sys.argv[1:]:
@@ -47,9 +49,11 @@ def _format_file(self, filename: Path) -> bool:
4749
for index, tokeninfo in enumerate(tokens):
4850
new_tokeninfo = tokeninfo
4951

52+
formatter_options = vars(self.config)
5053
if utils._is_docstring(new_tokeninfo, tokens[index - 1]):
5154
for formatter in formatting.FORMATTERS:
52-
new_tokeninfo = formatter.treat_token(new_tokeninfo)
55+
if formatter_options[formatter.name]:
56+
new_tokeninfo = formatter.treat_token(new_tokeninfo)
5357
changed_tokens.append(new_tokeninfo)
5458

5559
if tokeninfo != new_tokeninfo:

pydocstringformatter/utils/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
_parse_command_line_arguments,
33
_parse_toml_file,
44
_register_arguments,
5+
_register_arguments_formatters,
56
)
67
from pydocstringformatter.utils.exceptions import (
78
ParsingError,
@@ -22,6 +23,7 @@
2223
"ParsingError",
2324
"PydocstringFormatterError",
2425
"_register_arguments",
26+
"_register_arguments_formatters",
2527
"TomlParsingError",
2628
"_print_to_console",
2729
]

pydocstringformatter/utils/argument_parsing.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import tomli
66

7+
from pydocstringformatter.formatting.base import Formatter
78
from pydocstringformatter.utils.exceptions import TomlParsingError, UnrecognizedOption
89

910
OPTIONS_TYPES = {"write": "store_true"}
@@ -36,7 +37,26 @@ def _register_arguments(version: str) -> argparse.ArgumentParser:
3637
version=version,
3738
help="Show version number and exit",
3839
)
40+
return parser
41+
3942

43+
def _register_arguments_formatters(
44+
parser: argparse.ArgumentParser, formatters: List[Formatter]
45+
) -> argparse.ArgumentParser:
46+
"""Register a list of formatters, so they can all be deactivated or activated."""
47+
for formatter in formatters:
48+
name = formatter.name
49+
help_text = f"ctivate the {name} formatter"
50+
parser.add_argument(
51+
f"--{name}",
52+
action="store_true",
53+
dest=name,
54+
default=not formatter.optional,
55+
help=f"A{help_text} : {formatter.__doc__}",
56+
)
57+
parser.add_argument(
58+
f"--no-{name}", action="store_false", dest=name, help=f"Dea{help_text}"
59+
)
4060
return parser
4161

4262

tests/test_run.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,26 @@
22
import os
33
import sys
44
from pathlib import Path
5+
from typing import List
6+
from unittest.mock import patch
57

68
import pytest
79

810
import pydocstringformatter
11+
from pydocstringformatter.formatting import FORMATTERS
912

1013

1114
def test_no_arguments(capsys: pytest.CaptureFixture[str]) -> None:
1215
"""Test that we warn when no arguments are provided"""
1316
sys.argv = ["pydocstringformatter"]
1417
pydocstringformatter.run_docstring_formatter()
15-
output = capsys.readouterr()
16-
assert output.out.startswith("usage: pydocstringformatter [-h]")
17-
assert not output.err
18+
out, err = capsys.readouterr()
19+
assert out.startswith("usage: pydocstringformatter [-h]")
20+
assert "--beginning-quotes" in out
21+
assert "Activate the beginning-quotes formatter" in out
22+
assert "--no-beginning-quotes" in out
23+
assert "Deactivate the beginning-quotes formatter" in out
24+
assert not err
1825

1926

2027
def test_sys_agv_as_arguments(
@@ -106,3 +113,33 @@ def test_output_message_two_files(
106113
"""
107114
)
108115
assert not output.err
116+
117+
118+
@pytest.mark.parametrize(
119+
"args,should_format",
120+
[
121+
[[f"--no-{f.name}" for f in FORMATTERS], False],
122+
[[f"--{f.name}" for f in FORMATTERS], True],
123+
],
124+
)
125+
def test_optional_checker(
126+
args: List[str],
127+
should_format: bool,
128+
capsys: pytest.CaptureFixture[str],
129+
tmp_path: Path,
130+
) -> None:
131+
"""Test that optional check are activated or not depending on options."""
132+
bad_docstring = tmp_path / "bad_docstring.py"
133+
bad_docstring.write_text(f'"""{"a" * 120}\n{"b" * 120}"""')
134+
with patch.object(sys, "argv", ["pydocstringformatter", str(bad_docstring)] + args):
135+
pydocstringformatter.run_docstring_formatter()
136+
out, err = capsys.readouterr()
137+
assert not err
138+
if should_format:
139+
msg = "Nothing was modified, but all formatters are activated."
140+
assert "Nothing to do!" not in out
141+
expected = ["---", "@@", "+++"]
142+
assert all(e in out for e in expected), msg
143+
else:
144+
msg = "Something was modified, but all formatter are deactivated."
145+
assert "Nothing to do!" in out, msg

0 commit comments

Comments
 (0)