Skip to content

Commit 5cf33bc

Browse files
committed
Broaden support for core classes
1 parent c013cb0 commit 5cf33bc

File tree

6 files changed

+42
-16
lines changed

6 files changed

+42
-16
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66

7+
## Unreleased
8+
9+
### Added
10+
11+
- Add support for `click.Command`-like, `click.Group`-like and `click.Context`-like objects without requiring them to be actual subclasses. (Pull #82)
12+
13+
### Fixed
14+
15+
- Remove explicit reference to `click.BaseCommand` and `click.MultiCommand` objects in anticipation of their deprecation. (Pull #82)
16+
717
## 0.8.1 - 2023-09-18
818

919
### Fixed

mkdocs_click/_docs.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
def make_command_docs(
1717
prog_name: str,
18-
command: click.BaseCommand,
18+
command: click.Command,
1919
depth: int = 0,
2020
style: str = "plain",
2121
remove_ascii_art: bool = False,
@@ -42,7 +42,7 @@ def make_command_docs(
4242

4343
def _recursively_make_command_docs(
4444
prog_name: str,
45-
command: click.BaseCommand,
45+
command: click.Command,
4646
parent: click.Context | None = None,
4747
depth: int = 0,
4848
style: str = "plain",
@@ -90,20 +90,26 @@ def _recursively_make_command_docs(
9090

9191

9292
def _build_command_context(
93-
prog_name: str, command: click.BaseCommand, parent: click.Context | None
93+
prog_name: str, command: click.Command, parent: click.Context | None
9494
) -> click.Context:
95-
return click.Context(cast(click.Command, command), info_name=prog_name, parent=parent)
95+
return _get_context_class(command)(
96+
cast(click.Command, command), info_name=prog_name, parent=parent
97+
)
9698

9799

98-
def _get_sub_commands(command: click.Command, ctx: click.Context) -> list[click.Command]:
100+
def _get_sub_commands(
101+
command: click.Command | click.Group, ctx: click.Context
102+
) -> list[click.Command]:
99103
"""Return subcommands of a Click command."""
100104
subcommands = getattr(command, "commands", {})
101105
if subcommands:
102106
return list(subcommands.values())
103107

104-
if not isinstance(command, click.MultiCommand):
108+
if not _is_command_group(command):
105109
return []
106110

111+
command = cast(click.Group, command)
112+
107113
subcommands = []
108114

109115
for name in command.list_commands(ctx):
@@ -360,3 +366,13 @@ def _make_subcommands_links(
360366
help_string = "*No description was provided with this command.*"
361367
yield f"- *{command_bullet}*: {help_string}"
362368
yield ""
369+
370+
371+
def _get_context_class(command: click.Command) -> type[click.Context]:
372+
# https://github.com/pallets/click/blob/8.1.8/src/click/core.py#L859-L862
373+
return command.context_class
374+
375+
376+
def _is_command_group(command: click.Command) -> bool:
377+
# https://github.com/pallets/click/blob/8.1.8/src/click/core.py#L1806-L1811
378+
return isinstance(command, click.Group) or hasattr(command, "command_class")

mkdocs_click/_extension.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def replace_command_docs(has_attr_list: bool = False, **options: Any) -> Iterato
2222

2323
module = options["module"]
2424
command = options["command"]
25-
prog_name = options.get("prog_name", None)
25+
prog_name = options.get("prog_name")
2626
depth = int(options.get("depth", 0))
2727
style = options.get("style", "plain")
2828
remove_ascii_art = options.get("remove_ascii_art", False)

mkdocs_click/_loader.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
from ._exceptions import MkDocsClickException
1212

1313

14-
def load_command(module: str, attribute: str) -> click.BaseCommand:
14+
def load_command(module: str, attribute: str) -> click.Command:
1515
"""
1616
Load and return the Click command object located at '<module>:<attribute>'.
1717
"""
1818
command = _load_obj(module, attribute)
1919

20-
if not isinstance(command, click.BaseCommand):
20+
if not (isinstance(command, click.Command) or hasattr(command, "context_class")):
2121
raise MkDocsClickException(
22-
f"{attribute!r} must be a 'click.BaseCommand' object, got {type(command)}"
22+
f"{attribute!r} must be a 'click.Command'-like object, got {type(command)}"
2323
)
2424

2525
return command

tests/app/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def hidden():
5050
cli_named.add_command(hidden)
5151

5252

53-
class MultiCLI(click.MultiCommand):
53+
class MultiCLI(click.Group):
5454
def list_commands(self, ctx):
5555
return ["foo", "bar"]
5656

tests/test_docs.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def test_style_table(command, expected):
175175
assert output == expected
176176

177177

178-
class MultiCLI(click.MultiCommand):
178+
class MultiCLI(click.Group):
179179
def list_commands(self, ctx):
180180
return ["single-command"]
181181

@@ -190,9 +190,9 @@ def get_command(self, ctx, name):
190190
pytest.param(MultiCLI(help="Multi help"), id="no-name"),
191191
],
192192
)
193-
def test_custom_multicommand(multi):
193+
def test_custom_group(multi):
194194
"""
195-
Custom `MultiCommand` objects are supported (i.e. not just `Group` multi-commands).
195+
Custom `Group` objects are supported (i.e. not just `Group` multi-commands).
196196
"""
197197
expected = dedent(
198198
"""
@@ -242,9 +242,9 @@ def test_custom_multicommand(multi):
242242
pytest.param(MultiCLI(help="Multi help"), id="no-name"),
243243
],
244244
)
245-
def test_custom_multicommand_with_list_subcommands(multi):
245+
def test_custom_group_with_list_subcommands(multi):
246246
"""
247-
Custom `MultiCommand` objects are supported (i.e. not just `Group` multi-commands).
247+
Custom `Group` objects are supported (i.e. not just `Group` multi-commands).
248248
"""
249249
expected = dedent(
250250
"""

0 commit comments

Comments
 (0)