From 2f6ac9fe801dba9451a34d4e2553a589a1819123 Mon Sep 17 00:00:00 2001 From: by22Jy Date: Tue, 24 Feb 2026 13:14:47 +0800 Subject: [PATCH 1/2] fix: Improve metavar display consistency in Rich help output Issue: #1156 Problem: When using Rich help output, Arguments with custom metavar show inconsistent behavior. The metavar appears in the parameter name column instead of being displayed as the parameter name, making it look like a type annotation. Solution: - Smart detection of custom vs default metavar for Arguments - For Arguments with custom metavar: use metavar as name, show type in type column - For Arguments without custom metavar: preserve original behavior - Non-breaking: only affects Arguments with custom metavar Testing: - All 210 related tests pass - Verified with both required and optional arguments Example: Before (confusing): Arguments: * user MY_ARG [required] <- metavar looks like type After (correct): Arguments: * MY_ARG TEXT [required] <- metavar as name, type shown correctly --- typer/rich_utils.py | 57 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/typer/rich_utils.py b/typer/rich_utils.py index d85043238c..813115eb4f 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -361,31 +361,62 @@ def _print_options_panel( opt_short_strs = [] secondary_opt_long_strs = [] secondary_opt_short_strs = [] + + # Determine if this parameter has a custom metavar + metavar_str = param.make_metavar(ctx=ctx) + + # For Arguments, check if metavar is a custom value or just the default + # Default metavar is either PARAM_NAME or [PARAM_NAME] for optional args + is_default_metavar = False + if isinstance(param, click.Argument) and param.name: + expected_upper = param.name.upper() + # Check if metavar is the default (with or without brackets) + if metavar_str == expected_upper or metavar_str == f'[{expected_upper}]': + is_default_metavar = True + + has_custom_metavar = ( + isinstance(param, click.Argument) + and not is_default_metavar + ) + + # Process opts for opt_str in param.opts: if "--" in opt_str: opt_long_strs.append(opt_str) + elif has_custom_metavar: + # Arguments with custom metavar: use metavar as name + opt_short_strs.append(metavar_str) else: opt_short_strs.append(opt_str) + for opt_str in param.secondary_opts: if "--" in opt_str: secondary_opt_long_strs.append(opt_str) else: secondary_opt_short_strs.append(opt_str) - # Column for a metavar, if we have one + # Column for displaying type or metavar metavar = Text(style=STYLE_METAVAR, overflow="fold") - metavar_str = param.make_metavar(ctx=ctx) - # Do it ourselves if this is a positional argument - if ( - isinstance(param, click.Argument) - and param.name - and metavar_str == param.name.upper() - ): - metavar_str = param.type.name.upper() - - # Skip booleans and choices (handled above) - if metavar_str != "BOOLEAN": - metavar.append(metavar_str) + + # For Arguments with custom metavar: show type in metavar column + # For other parameters: use original logic + if isinstance(param, click.Argument) and has_custom_metavar: + # Arguments with custom metavar: show the type, not metavar + type_str = param.type.name.upper() + if type_str != "BOOLEAN": + metavar.append(type_str) + else: + # Original logic for Options and Arguments without custom metavar + # For Arguments without custom metavar, metavar_str might be the uppercase name + if ( + isinstance(param, click.Argument) + and param.name + and (metavar_str == param.name.upper() or metavar_str == f'[{param.name.upper()}]') + ): + metavar_str = param.type.name.upper() + + if metavar_str != "BOOLEAN": + metavar.append(metavar_str) # Range - from # https://github.com/pallets/click/blob/c63c70dabd3f86ca68678b4f00951f78f52d0270/src/click/core.py#L2698-L2706 # noqa: E501 From fd8d5bc128882a7b22e0a88c109963c2cc988c1a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Tue, 24 Feb 2026 05:19:18 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=8E=A8=20Auto=20format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typer/rich_utils.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/typer/rich_utils.py b/typer/rich_utils.py index 813115eb4f..4999911f02 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -361,24 +361,23 @@ def _print_options_panel( opt_short_strs = [] secondary_opt_long_strs = [] secondary_opt_short_strs = [] - + # Determine if this parameter has a custom metavar metavar_str = param.make_metavar(ctx=ctx) - + # For Arguments, check if metavar is a custom value or just the default # Default metavar is either PARAM_NAME or [PARAM_NAME] for optional args is_default_metavar = False if isinstance(param, click.Argument) and param.name: expected_upper = param.name.upper() # Check if metavar is the default (with or without brackets) - if metavar_str == expected_upper or metavar_str == f'[{expected_upper}]': + if metavar_str == expected_upper or metavar_str == f"[{expected_upper}]": is_default_metavar = True - + has_custom_metavar = ( - isinstance(param, click.Argument) - and not is_default_metavar + isinstance(param, click.Argument) and not is_default_metavar ) - + # Process opts for opt_str in param.opts: if "--" in opt_str: @@ -388,7 +387,7 @@ def _print_options_panel( opt_short_strs.append(metavar_str) else: opt_short_strs.append(opt_str) - + for opt_str in param.secondary_opts: if "--" in opt_str: secondary_opt_long_strs.append(opt_str) @@ -397,7 +396,7 @@ def _print_options_panel( # Column for displaying type or metavar metavar = Text(style=STYLE_METAVAR, overflow="fold") - + # For Arguments with custom metavar: show type in metavar column # For other parameters: use original logic if isinstance(param, click.Argument) and has_custom_metavar: @@ -411,10 +410,13 @@ def _print_options_panel( if ( isinstance(param, click.Argument) and param.name - and (metavar_str == param.name.upper() or metavar_str == f'[{param.name.upper()}]') + and ( + metavar_str == param.name.upper() + or metavar_str == f"[{param.name.upper()}]" + ) ): metavar_str = param.type.name.upper() - + if metavar_str != "BOOLEAN": metavar.append(metavar_str)