Skip to content

Add an external formatter API to be able to launch Run with custom formatters #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion pydocstringformatter/formatting/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__all__ = ["FORMATTERS"]
__all__ = ["FORMATTERS", "Formatter"]

from typing import List

Expand Down
17 changes: 11 additions & 6 deletions pydocstringformatter/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,28 @@
import sys
import tokenize
from pathlib import Path
from typing import List, Union
from typing import List, Optional, Union

from pydocstringformatter import __version__, formatting, utils


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

def __init__(self, argv: Union[List[str], None]) -> None:
def __init__(
self,
argv: Union[List[str], None],
formatters: Optional[List[formatting.Formatter]] = None,
) -> None:
if formatters is None:
formatters = formatting.FORMATTERS
self.formatters = formatters
self.arg_parser = utils._register_arguments(__version__)
utils._register_arguments_formatters(self.arg_parser, formatting.FORMATTERS)
utils._register_arguments_formatters(self.arg_parser, self.formatters)
self.config = argparse.Namespace()

if argv := argv or sys.argv[1:]:
utils._parse_options(
self.arg_parser, self.config, argv, formatting.FORMATTERS
)
utils._parse_options(self.arg_parser, self.config, argv, self.formatters)
self._check_files(self.config.files)
else:
self.arg_parser.print_help()
Expand Down
26 changes: 25 additions & 1 deletion tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
import os
import sys
from pathlib import Path
from tokenize import TokenInfo
from typing import List

import pytest

import pydocstringformatter
from pydocstringformatter.formatting import FORMATTERS
from pydocstringformatter.formatting import FORMATTERS, Formatter
from pydocstringformatter.run import _Run


def test_no_arguments(capsys: pytest.CaptureFixture[str]) -> None:
Expand Down Expand Up @@ -116,6 +118,28 @@ def test_output_message_two_files(
assert not output.err


def test_optional_formatter(capsys: pytest.CaptureFixture[str]) -> None:
"""If a Formatter is optional we handle that in _load_default_formatter."""

class OptionalFormatter(Formatter):
"""Non-optional docstring."""

optional = True
name = "optional-formatter"

def treat_token(self, tokeninfo: TokenInfo) -> TokenInfo:
"""This does nothing"""
return tokeninfo

with pytest.raises(SystemExit):
_Run(argv=["--help"], formatters=[OptionalFormatter()])
out, err = capsys.readouterr()
assert not err
assert "--optional-formatter" in out
assert "optional docstring." in out
assert "--no-optional-formatter" in out


@pytest.mark.parametrize(
"args,should_format",
[
Expand Down