Skip to content
29 changes: 29 additions & 0 deletions tests/test_rich_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 5 additions & 3 deletions typer/rich_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading