From 31d10fffe95110ac97bdf666bfc422698f766785 Mon Sep 17 00:00:00 2001 From: James McKinney <26463+jpmckinney@users.noreply.github.com> Date: Thu, 15 Aug 2024 13:09:26 -0400 Subject: [PATCH 1/3] fix: Do not override the command order of a custom group's list_commands() --- sphinxcontrib/typer/__init__.py | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/sphinxcontrib/typer/__init__.py b/sphinxcontrib/typer/__init__.py index 214e39c..a5c5366 100644 --- a/sphinxcontrib/typer/__init__.py +++ b/sphinxcontrib/typer/__init__.py @@ -70,23 +70,20 @@ def get_function(function: t.Union[str, t.Callable[..., t.Any]]): def _filter_commands(ctx: click.Context, cmd_filter: t.Optional[t.List[str]] = None): - return sorted( - [ - cmd - for name, cmd in getattr( - ctx.command, - "commands", - { - name: ctx.command.get_command(ctx, name) - for name in getattr(ctx.command, "list_commands", lambda _: [])(ctx) - or cmd_filter - or [] - }, - ).items() - if not cmd_filter or name in cmd_filter - ], - key=lambda item: item.name, - ) + return [ + cmd + for name, cmd in getattr( + ctx.command, + "commands", + { + name: ctx.command.get_command(ctx, name) + for name in getattr(ctx.command, "list_commands", lambda _: [])(ctx) + or cmd_filter + or [] + }, + ).items() + if not cmd_filter or name in cmd_filter + ] class RenderTarget(str, Enum): From baade28c3ce49407a49f8f1f4f36f43989ee06e2 Mon Sep 17 00:00:00 2001 From: Brian Kohan Date: Thu, 15 Aug 2024 16:37:59 -0700 Subject: [PATCH 2/3] simply filter function, update changelog, update tests, add test of order reversal --- sphinxcontrib/typer/__init__.py | 19 +++---------------- tests/click/aliases/aliases.py | 3 +++ tests/click/completion/completion.py | 7 ++++++- tests/click/imagepipe/imagepipe.py | 7 ++++++- tests/click/naval/naval.py | 9 +++++++-- tests/click/repo/repo.py | 7 ++++++- tests/click/termui/termui.py | 7 ++++++- tests/tests.py | 3 ++- 8 files changed, 39 insertions(+), 23 deletions(-) mode change 100644 => 100755 tests/click/termui/termui.py diff --git a/sphinxcontrib/typer/__init__.py b/sphinxcontrib/typer/__init__.py index a5c5366..97b8ee9 100644 --- a/sphinxcontrib/typer/__init__.py +++ b/sphinxcontrib/typer/__init__.py @@ -54,7 +54,7 @@ __version__ = ".".join(str(i) for i in VERSION) __author__ = "Brian Kohan" __license__ = "MIT" -__copyright__ = "Copyright 2023 Brian Kohan" +__copyright__ = "Copyright 2023-2024 Brian Kohan" SELENIUM_DEFAULT_WINDOW_WIDTH = 1920 @@ -69,21 +69,8 @@ def get_function(function: t.Union[str, t.Callable[..., t.Any]]): return getattr(import_module(".".join(parts[0:-1])), parts[-1]) -def _filter_commands(ctx: click.Context, cmd_filter: t.Optional[t.List[str]] = None): - return [ - cmd - for name, cmd in getattr( - ctx.command, - "commands", - { - name: ctx.command.get_command(ctx, name) - for name in getattr(ctx.command, "list_commands", lambda _: [])(ctx) - or cmd_filter - or [] - }, - ).items() - if not cmd_filter or name in cmd_filter - ] +def _filter_commands(ctx: click.Context, cmd_filter: t.List[str]): + return [ctx.command.commands[cmd_name] for cmd_name in cmd_filter] class RenderTarget(str, Enum): diff --git a/tests/click/aliases/aliases.py b/tests/click/aliases/aliases.py index 2a055ae..86b4c68 100644 --- a/tests/click/aliases/aliases.py +++ b/tests/click/aliases/aliases.py @@ -39,6 +39,9 @@ class AliasedGroup(click.Group): file and with a bit of magic. """ + def list_commands(self, ctx): + return reversed(sorted(super().list_commands(ctx))) + def get_command(self, ctx, cmd_name): # Step one: bulitin commands as normal rv = click.Group.get_command(self, ctx, cmd_name) diff --git a/tests/click/completion/completion.py b/tests/click/completion/completion.py index 578906a..70ff74f 100644 --- a/tests/click/completion/completion.py +++ b/tests/click/completion/completion.py @@ -4,7 +4,12 @@ from click.shell_completion import CompletionItem -@click.group() +class AlphOrderedGroup(click.Group): + def list_commands(self, ctx): + return sorted(super().list_commands(ctx)) + + +@click.group(cls=AlphOrderedGroup) def cli(): pass diff --git a/tests/click/imagepipe/imagepipe.py b/tests/click/imagepipe/imagepipe.py index 4289593..350af41 100644 --- a/tests/click/imagepipe/imagepipe.py +++ b/tests/click/imagepipe/imagepipe.py @@ -7,7 +7,12 @@ import click -@click.group(chain=True) +class AlphOrderedGroup(click.Group): + def list_commands(self, ctx): + return sorted(super().list_commands(ctx)) + + +@click.group(cls=AlphOrderedGroup, chain=True) def cli(): """This script processes a bunch of images through pillow in a unix pipe. One commands feeds into the next. diff --git a/tests/click/naval/naval.py b/tests/click/naval/naval.py index 3ad5e26..46a0b76 100644 --- a/tests/click/naval/naval.py +++ b/tests/click/naval/naval.py @@ -1,7 +1,12 @@ import click -@click.group() +class AlphOrderedGroup(click.Group): + def list_commands(self, ctx): + return sorted(super().list_commands(ctx)) + + +@click.group(cls=AlphOrderedGroup) @click.version_option() def cli(): """Naval Fate. @@ -12,7 +17,7 @@ def cli(): """ -@cli.group() +@cli.group(cls=AlphOrderedGroup) def ship(): """Manages ships.""" diff --git a/tests/click/repo/repo.py b/tests/click/repo/repo.py index 7e87c27..50db00f 100644 --- a/tests/click/repo/repo.py +++ b/tests/click/repo/repo.py @@ -5,6 +5,11 @@ import click +class AlphOrderedGroup(click.Group): + def list_commands(self, ctx): + return sorted(super().list_commands(ctx)) + + class Repo: def __init__(self, home): self.home = home @@ -23,7 +28,7 @@ def __repr__(self): pass_repo = click.make_pass_decorator(Repo) -@click.group() +@click.group(cls=AlphOrderedGroup) @click.option( "--repo-home", envvar="REPO_HOME", diff --git a/tests/click/termui/termui.py b/tests/click/termui/termui.py old mode 100644 new mode 100755 index bc5099a..da3df86 --- a/tests/click/termui/termui.py +++ b/tests/click/termui/termui.py @@ -5,7 +5,12 @@ import click -@click.group() +class AlphOrderedGroup(click.Group): + def list_commands(self, ctx): + return sorted(super().list_commands(ctx)) + + +@click.group(cls=AlphOrderedGroup) def cli(): """This script showcases different terminal UI helpers in Click.""" pass diff --git a/tests/tests.py b/tests/tests.py index 9df4e81..2adba96 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -476,7 +476,8 @@ def test_click_ex_aliases(): bld_dir, html = build_click_example("aliases", "html") - subcommands = ["alias", "clone", "commit", "pull", "push", "status"] + # we test that list_commands order is honored + subcommands = reversed(["alias", "clone", "commit", "pull", "push", "status"]) helps = [ get_click_ex_help("aliases"), *[get_click_ex_help("aliases", *cmd.split()) for cmd in subcommands], From b2566b2aae168cfc6a366a22ee8b2c40274b02d0 Mon Sep 17 00:00:00 2001 From: Brian Kohan Date: Thu, 15 Aug 2024 16:38:14 -0700 Subject: [PATCH 3/3] update changelog --- doc/source/changelog.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/doc/source/changelog.rst b/doc/source/changelog.rst index 91492bf..266d888 100644 --- a/doc/source/changelog.rst +++ b/doc/source/changelog.rst @@ -2,6 +2,11 @@ Change Log ========== +v0.3.4 (15-AUG-2024) +==================== +* `list_commands order should be honored when generated nested sections for subcommands. `_ + + v0.3.3 (15-JUL-2024) ====================