From 3bc7d2560f4c8f86e058c9ea1d1e682d14c5107c Mon Sep 17 00:00:00 2001 From: Sofie Van Landeghem Date: Tue, 9 Apr 2024 19:10:18 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20Rich=20formatting=20with?= =?UTF-8?q?=20no=20commands=20(#796)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_rich_utils.py | 14 ++++++++++++++ typer/rich_utils.py | 3 ++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/test_rich_utils.py b/tests/test_rich_utils.py index b6da8995f6..c62e3512aa 100644 --- a/tests/test_rich_utils.py +++ b/tests/test_rich_utils.py @@ -36,3 +36,17 @@ def secondary(): assert "Hello World" in result.stdout result = runner.invoke(app, ["secondary"]) assert "Hello Secondary World" in result.stdout + + +def test_rich_help_no_commands(): + """Ensure that the help still works for a Typer instance with no commands, but with a callback.""" + app = typer.Typer(help="My cool Typer app") + + @app.callback(invoke_without_command=True, no_args_is_help=True) + def main() -> None: + return None # pragma: no cover + + result = runner.invoke(app, ["--help"]) + + assert result.exit_code == 0 + assert "Show this message" in result.stdout diff --git a/typer/rich_utils.py b/typer/rich_utils.py index 2b91f265ab..cf0538e914 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -644,7 +644,8 @@ def rich_format_help( len(command.name or "") for commands in panel_to_commands.values() for command in commands - ] + ], + default=0, ) # Print each command group panel