Skip to content

Commit cdc29e2

Browse files
mattmess1221pre-commit-ci[bot]svlandegtiangolo
authored
πŸ› Avoid printing default: None in the help section when using Rich (#1120)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sofie Van Landeghem <[email protected]> Co-authored-by: svlandeg <[email protected]> Co-authored-by: SebastiΓ‘n RamΓ­rez <[email protected]>
1 parent 50b1018 commit cdc29e2

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

β€Žtests/test_rich_utils.pyβ€Ž

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,32 @@ def main() -> None:
5050

5151
assert result.exit_code == 0
5252
assert "Show this message" in result.stdout
53+
54+
55+
def test_rich_doesnt_print_None_default():
56+
app = typer.Typer(rich_markup_mode="rich")
57+
58+
@app.command()
59+
def main(
60+
name: str,
61+
option_1: str = typer.Option(
62+
"option_1_default",
63+
),
64+
option_2: str = typer.Option(
65+
...,
66+
),
67+
):
68+
print(f"Hello {name}")
69+
print(f"First: {option_1}")
70+
print(f"Second: {option_2}")
71+
72+
result = runner.invoke(app, ["--help"])
73+
assert "Usage" in result.stdout
74+
assert "name" in result.stdout
75+
assert "option-1" in result.stdout
76+
assert "option-2" in result.stdout
77+
assert result.stdout.count("[default: None]") == 0
78+
result = runner.invoke(app, ["Rick", "--option-2=Morty"])
79+
assert "Hello Rick" in result.stdout
80+
assert "First: option_1_default" in result.stdout
81+
assert "Second: Morty" in result.stdout

β€Žtyper/rich_utils.pyβ€Ž

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,11 @@ def _get_parameter_help(
288288
# Default value
289289
# This uses Typer's specific param._get_default_string
290290
if isinstance(param, (TyperOption, TyperArgument)):
291-
if param.show_default:
292-
show_default_is_str = isinstance(param.show_default, str)
293-
default_value = param._extract_default_help_str(ctx=ctx)
291+
default_value = param._extract_default_help_str(ctx=ctx)
292+
show_default_is_str = isinstance(param.show_default, str)
293+
if show_default_is_str or (
294+
default_value is not None and (param.show_default or ctx.show_default)
295+
):
294296
default_str = param._get_default_string(
295297
ctx=ctx,
296298
show_default_is_str=show_default_is_str,

0 commit comments

Comments
Β (0)