diff --git a/tests/test_rich_utils.py b/tests/test_rich_utils.py index c62e3512aa..fbcfdde4b8 100644 --- a/tests/test_rich_utils.py +++ b/tests/test_rich_utils.py @@ -50,3 +50,32 @@ def main() -> None: assert result.exit_code == 0 assert "Show this message" in result.stdout + + +def test_rich_doesnt_print_None_default(): + app = typer.Typer(rich_markup_mode="rich") + + @app.command() + def main( + name: str, + option_1: str = typer.Option( + "option_1_default", + ), + option_2: str = typer.Option( + ..., + ), + ): + print(f"Hello {name}") + print(f"First: {option_1}") + print(f"Second: {option_2}") + + result = runner.invoke(app, ["--help"]) + assert "Usage" in result.stdout + assert "name" in result.stdout + assert "option-1" in result.stdout + assert "option-2" in result.stdout + assert result.stdout.count("[default: None]") == 0 + result = runner.invoke(app, ["Rick", "--option-2=Morty"]) + assert "Hello Rick" in result.stdout + assert "First: option_1_default" in result.stdout + assert "Second: Morty" in result.stdout diff --git a/typer/rich_utils.py b/typer/rich_utils.py index c6f2caaa48..404e97503b 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -288,9 +288,11 @@ def _get_parameter_help( # Default value # This uses Typer's specific param._get_default_string if isinstance(param, (TyperOption, TyperArgument)): - if param.show_default: - show_default_is_str = isinstance(param.show_default, str) - default_value = param._extract_default_help_str(ctx=ctx) + default_value = param._extract_default_help_str(ctx=ctx) + show_default_is_str = isinstance(param.show_default, str) + if show_default_is_str or ( + default_value is not None and (param.show_default or ctx.show_default) + ): default_str = param._get_default_string( ctx=ctx, show_default_is_str=show_default_is_str,