Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Current usage of ``pydocstringformatter``:
[--exit-code] [--max-summary-lines int]
[--summary-quotes-same-line]
[--max-line-length int]
[--style {pep257} [{pep257} ...]]
[--strip-whitespaces --no-strip-whitespaces]
[--split-summary-body --no-split-summary-body]
[--linewrap-full-docstring --no-linewrap-full-docstring]
Expand Down Expand Up @@ -44,6 +45,9 @@ Current usage of ``pydocstringformatter``:
is enforced for single line docstrings.
--max-line-length int
Maximum line length of docstrings.
--style {pep257} [{pep257} ...]
Docstring styles that are used in the project. Can be
more than one.
Comment on lines +49 to +50
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It means we'll have to detect which style is which given a docstring. We have something who does that in pylint, we might extract it to make something generic...

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's the next step.

Although I don't really like the difficult regexes pylint uses. I'd rather build them from scratch here and see if we can keep them a little bit simpler.

We need less information that pylint needs to extract from the regexes anyway as we don't really care about the content of the docstrings...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather build them from scratch here and see if we can keep them a little bit simpler.

Then it's an opportunity to remove the complicated regexes from pylint and instead of extracting we replace them 😄


default formatters:
these formatters are turned on by default
Expand Down
16 changes: 15 additions & 1 deletion pydocstringformatter/_configuration/arguments_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,23 @@ def register_arguments(self, version: str) -> None:
metavar="int",
)

self.configuration_group.add_argument(
"--style",
action="extend",
type=str,
nargs="+",
choices=["pep257"],
help="Docstring styles that are used in the project. Can be more than one.",
)

def parse_options(
self,
argv: list[str],
) -> None:
"""Load all default option values.

The order of parsing is:
1. configuration files, 2. command line arguments.
1. configuration files, 2. command line arguments, 3. set default values.
"""
# pylint: disable=protected-access
toml_parsing.parse_toml_file(self.parser, self.namespace)
Expand All @@ -134,6 +143,11 @@ def parse_options(
self.parser, self.namespace, argv
)

# 'style' uses the 'extend' action. If we use a normal default value,
# and the default gets supplied as well style == ["pep257", "pep257"].
if self.namespace.style is None:
self.namespace.style = ["pep257"]

def print_help(self) -> None:
"""Print the help or usage message."""
self.parser.print_help()
25 changes: 25 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import pydocstringformatter
from pydocstringformatter._utils import exceptions
from pydocstringformatter.run import _Run

HERE = Path(__file__)
CONFIG_DATA = HERE.parent / "data" / "config"
Expand Down Expand Up @@ -225,3 +226,27 @@ def test_quiet_argument(capsys: pytest.CaptureFixture[str], test_file: str) -> N
output = capsys.readouterr()
assert not output.out
assert not output.err


class TestStyleOption:
"""Tests for the --style option."""

def test_style_default(self, test_file: str) -> None:
"""Test that the default value of --style is pep257."""
run = _Run([test_file])
assert run.config.style == ["pep257"]

def test_style_pep257(self, test_file: str) -> None:
"""Test that we don't duplicate the default value if we pass it again."""
run = _Run([test_file, "--style", "pep257"])
assert run.config.style == ["pep257"]

def test_style_invalid_choice(self, capsys: pytest.CaptureFixture[str]) -> None:
"""Test that we correctly reject invalid styles."""
with pytest.raises(SystemExit) as excinfo:
_Run(["--style", "invalid"])
assert excinfo.value.code == 2

output = capsys.readouterr()
assert not output.out
assert "--style: invalid choice: 'invalid'" in output.err