Skip to content

Commit

Permalink
Switch line-length to 100
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin committed Sep 16, 2023
1 parent e1a9fe6 commit efef045
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 16 deletions.
22 changes: 17 additions & 5 deletions mkdocs_click/_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ def _recursively_make_command_docs(
)


def _build_command_context(prog_name: str, command: click.BaseCommand, parent: click.Context | None) -> click.Context:
def _build_command_context(
prog_name: str, command: click.BaseCommand, parent: click.Context | None
) -> click.Context:
return click.Context(cast(click.Command, command), info_name=prog_name, parent=parent)


Expand Down Expand Up @@ -195,21 +197,27 @@ def _make_usage(ctx: click.Context) -> Iterator[str]:
yield ""


def _make_options(ctx: click.Context, style: str = "plain", show_hidden: bool = False) -> Iterator[str]:
def _make_options(
ctx: click.Context, style: str = "plain", show_hidden: bool = False
) -> Iterator[str]:
"""Create the Markdown lines describing the options for the command."""

if style == "plain":
return _make_plain_options(ctx, show_hidden=show_hidden)
elif style == "table":
return _make_table_options(ctx, show_hidden=show_hidden)
else:
raise MkDocsClickException(f"{style} is not a valid option style, which must be either `plain` or `table`.")
raise MkDocsClickException(
f"{style} is not a valid option style, which must be either `plain` or `table`."
)


@contextmanager
def _show_options(ctx: click.Context) -> Iterator[None]:
"""Context manager that temporarily shows all hidden options."""
options = [opt for opt in ctx.command.get_params(ctx) if isinstance(opt, click.Option) and opt.hidden]
options = [
opt for opt in ctx.command.get_params(ctx) if isinstance(opt, click.Option) and opt.hidden
]

try:
for option in options:
Expand Down Expand Up @@ -340,7 +348,11 @@ def _make_subcommands_links(
ctx = _build_command_context(command_name, command, parent)
if ctx.command.hidden and not show_hidden:
continue
command_bullet = command_name if not has_attr_list else f"[{command_name}](#{slugify(ctx.command_path, '-')})"
command_bullet = (
command_name
if not has_attr_list
else f"[{command_name}](#{slugify(ctx.command_path, '-')})"
)
help_string = ctx.command.short_help or ctx.command.help
if help_string is not None:
help_string = help_string.splitlines()[0]
Expand Down
8 changes: 6 additions & 2 deletions mkdocs_click/_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,18 @@ def replace_command_docs(has_attr_list: bool = False, **options: Any) -> Iterato
class ClickProcessor(Preprocessor):
def __init__(self, md: Any) -> None:
super().__init__(md)
self._has_attr_list = any(isinstance(ext, AttrListExtension) for ext in md.registeredExtensions)
self._has_attr_list = any(
isinstance(ext, AttrListExtension) for ext in md.registeredExtensions
)

def run(self, lines: list[str]) -> list[str]:
return list(
replace_blocks(
lines,
title="mkdocs-click",
replace=lambda **options: replace_command_docs(has_attr_list=self._has_attr_list, **options),
replace=lambda **options: replace_command_docs(
has_attr_list=self._has_attr_list, **options
),
)
)

Expand Down
4 changes: 3 additions & 1 deletion mkdocs_click/_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ def load_command(module: str, attribute: str) -> click.BaseCommand:
command = _load_obj(module, attribute)

if not isinstance(command, click.BaseCommand):
raise MkDocsClickException(f"{attribute!r} must be a 'click.BaseCommand' object, got {type(command)}")
raise MkDocsClickException(
f"{attribute!r} must be a 'click.BaseCommand' object, got {type(command)}"
)

return command

Expand Down
4 changes: 3 additions & 1 deletion mkdocs_click/_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
from typing import Callable, Iterable, Iterator


def replace_blocks(lines: Iterable[str], title: str, replace: Callable[..., Iterable[str]]) -> Iterator[str]:
def replace_blocks(
lines: Iterable[str], title: str, replace: Callable[..., Iterable[str]]
) -> Iterator[str]:
"""
Find blocks of lines in the form of:
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ format = [
]

[tool.black]
line-length = 120
line-length = 100

[tool.isort]
profile = "black"
line_length = 120
line_length = 100

[tool.ruff]
select = [
Expand Down
7 changes: 5 additions & 2 deletions tests/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def test_prog_name():

def test_style_invalid():
with pytest.raises(
MkDocsClickException, match="invalid is not a valid option style, which must be either `plain` or `table`."
MkDocsClickException,
match="invalid is not a valid option style, which must be either `plain` or `table`.",
):
list(make_command_docs("hello", hello, style="invalid"))

Expand Down Expand Up @@ -298,7 +299,9 @@ def test_show_hidden_option(show_hidden, style):
def _test_cmd(hidden):
"""Test cmd."""

output = "\n".join(make_command_docs("_test_cmd", _test_cmd, style=style, show_hidden=show_hidden))
output = "\n".join(
make_command_docs("_test_cmd", _test_cmd, style=style, show_hidden=show_hidden)
)
assert ("--hidden" in output) == show_hidden


Expand Down
4 changes: 3 additions & 1 deletion tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"module, command, exc",
[
pytest.param("tests.app.cli", "cli", None, id="ok"),
pytest.param("tests.app.cli", "doesnotexist", MkDocsClickException, id="command-does-not-exist"),
pytest.param(
"tests.app.cli", "doesnotexist", MkDocsClickException, id="command-does-not-exist"
),
pytest.param("doesnotexist", "cli", ImportError, id="module-does-not-exist"),
pytest.param("tests.app.cli", "NOT_A_COMMAND", MkDocsClickException, id="not-a-command"),
],
Expand Down
10 changes: 8 additions & 2 deletions tests/test_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ def test_replace_options():
bar
""".strip()

output = list(replace_blocks(source.splitlines(), title="target", replace=lambda **options: [str(options)]))
output = list(
replace_blocks(
source.splitlines(), title="target", replace=lambda **options: [str(options)]
)
)
assert output == expected.splitlines()


Expand All @@ -46,7 +50,9 @@ def test_replace_no_options():
bar
""".strip()

output = list(replace_blocks(source.splitlines(), title="target", replace=lambda **options: ["> mock"]))
output = list(
replace_blocks(source.splitlines(), title="target", replace=lambda **options: ["> mock"])
)
assert output == expected.splitlines()


Expand Down

0 comments on commit efef045

Please sign in to comment.