Skip to content

🐛 Avoid printing default: None in the help section when using Rich #1120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: on master, the count is 2.

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 @@ -289,9 +289,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