From 9465f963eb446a9ac84158b8217c505fb9b44679 Mon Sep 17 00:00:00 2001 From: Travis Hathaway Date: Sat, 22 Nov 2025 11:41:21 +0100 Subject: [PATCH 1/3] providing a way to override _print_options_panel and _print_commands_panel functions --- typer/rich_utils.py | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/typer/rich_utils.py b/typer/rich_utils.py index d4c3676aea..a1e0ec4b4a 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -6,7 +6,7 @@ from collections import defaultdict from gettext import gettext as _ from os import getenv -from typing import Any, DefaultDict, Dict, Iterable, List, Optional, Union +from typing import Any, Callable, DefaultDict, Dict, Iterable, List, Optional, Union import click from rich import box @@ -549,11 +549,39 @@ def _print_commands_panel( ) +# Define acceptable function to pass as print_options_panel_func +PrintOptionsPanelFunc = Callable[ + [ + str, + Union[List[click.Option], List[click.Argument]], + click.Context, + MarkupMode, + Console + ], + None +] + + +# Define acceptable function to pass as print_options_panel_func +PrintCommandsPanelFunc = Callable[ + [ + str, + List[click.Command], + MarkupMode, + Console, + int + ], + None +] + + def rich_format_help( *, obj: Union[click.Command, click.Group], ctx: click.Context, markup_mode: MarkupMode, + print_options_panel_func: PrintOptionsPanelFunc = _print_options_panel, + print_commands_panel_func: PrintCommandsPanelFunc = _print_commands_panel, ) -> None: """Print nicely formatted help text using rich. @@ -602,7 +630,7 @@ def rich_format_help( ) panel_to_options[panel_name].append(param) default_arguments = panel_to_arguments.get(ARGUMENTS_PANEL_TITLE, []) - _print_options_panel( + print_options_panel_func( name=ARGUMENTS_PANEL_TITLE, params=default_arguments, ctx=ctx, @@ -613,7 +641,7 @@ def rich_format_help( if panel_name == ARGUMENTS_PANEL_TITLE: # Already printed above continue - _print_options_panel( + print_options_panel_func( name=panel_name, params=arguments, ctx=ctx, @@ -621,7 +649,7 @@ def rich_format_help( console=console, ) default_options = panel_to_options.get(OPTIONS_PANEL_TITLE, []) - _print_options_panel( + print_options_panel_func( name=OPTIONS_PANEL_TITLE, params=default_options, ctx=ctx, @@ -632,7 +660,7 @@ def rich_format_help( if panel_name == OPTIONS_PANEL_TITLE: # Already printed above continue - _print_options_panel( + print_options_panel_func( name=panel_name, params=options, ctx=ctx, @@ -663,7 +691,7 @@ def rich_format_help( # Print each command group panel default_commands = panel_to_commands.get(COMMANDS_PANEL_TITLE, []) - _print_commands_panel( + print_commands_panel_func( name=COMMANDS_PANEL_TITLE, commands=default_commands, markup_mode=markup_mode, @@ -674,7 +702,7 @@ def rich_format_help( if panel_name == COMMANDS_PANEL_TITLE: # Already printed above continue - _print_commands_panel( + print_commands_panel_func( name=panel_name, commands=commands, markup_mode=markup_mode, From c1974f077c19dc9e874a3d8b527a8d6e5d319593 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 22 Nov 2025 11:02:28 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20for?= =?UTF-8?q?mat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typer/rich_utils.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/typer/rich_utils.py b/typer/rich_utils.py index a1e0ec4b4a..88dcaa9a09 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -556,22 +556,15 @@ def _print_commands_panel( Union[List[click.Option], List[click.Argument]], click.Context, MarkupMode, - Console + Console, ], - None + None, ] # Define acceptable function to pass as print_options_panel_func PrintCommandsPanelFunc = Callable[ - [ - str, - List[click.Command], - MarkupMode, - Console, - int - ], - None + [str, List[click.Command], MarkupMode, Console, int], None ] From a82472aad82cd7901faa2232bef497f8a7d87916 Mon Sep 17 00:00:00 2001 From: Travis Hathaway Date: Sat, 22 Nov 2025 18:53:50 +0100 Subject: [PATCH 3/3] fixing mypy issues --- typer/rich_utils.py | 60 ++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/typer/rich_utils.py b/typer/rich_utils.py index a1e0ec4b4a..d420936b20 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -6,7 +6,7 @@ from collections import defaultdict from gettext import gettext as _ from os import getenv -from typing import Any, Callable, DefaultDict, Dict, Iterable, List, Optional, Union +from typing import Any, DefaultDict, Dict, Iterable, List, Optional, Protocol, Union import click from rich import box @@ -549,30 +549,40 @@ def _print_commands_panel( ) -# Define acceptable function to pass as print_options_panel_func -PrintOptionsPanelFunc = Callable[ - [ - str, - Union[List[click.Option], List[click.Argument]], - click.Context, - MarkupMode, - Console - ], - None -] - - -# Define acceptable function to pass as print_options_panel_func -PrintCommandsPanelFunc = Callable[ - [ - str, - List[click.Command], - MarkupMode, - Console, - int - ], - None -] +class PrintOptionsPanelFunc(Protocol): + """ + Define acceptable function to pass as print_options_panel_func + + TODO: switch to using NamedArg once minimum supported python is 3.12 + """ + + def __call__( + self, + *, + name: str, + params: Union[List[click.Option], List[click.Argument]], + ctx: click.Context, + markup_mode: MarkupMode, + console: Console, + ) -> None: ... + + +class PrintCommandsPanelFunc(Protocol): + """ + Define acceptable function to pass as print_options_panel_func + + TODO: switch to using NamedArg once minimum supported python is 3.12 + """ + + def __call__( + self, + *, + name: str, + commands: List[click.Command], + markup_mode: MarkupMode, + console: Console, + cmd_len: int, + ) -> None: ... def rich_format_help(