Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Matthew Messinger · committed by GitHub · parent 50b1018 · Browse files · 2 files+34 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com>
  • Co-authored-by: svlandeg <svlandeg@github.com>
  • Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>

Diff stats

2 files+34 added−3 removed

  • tests:+29 added−0 removed
  • other:+5 added−3 removed

2 files+34 added−3 removed

modified:tests/test_rich_utils.py+29 added
48 unmodified lines
49 result = runner.invoke(app, ["--help"])5051 assert result.exit_code == 052 assert "Show this message" in result.stdout535455def test_rich_doesnt_print_None_default():56 app = typer.Typer(rich_markup_mode="rich")5758 @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}")7172 result = runner.invoke(app, ["--help"])73 assert "Usage" in result.stdout74 assert "name" in result.stdout75 assert "option-1" in result.stdout76 assert "option-2" in result.stdout77 assert result.stdout.count("[default: None]") == 078 result = runner.invoke(app, ["Rick", "--option-2=Morty"])79 assert "Hello Rick" in result.stdout80 assert "First: option_1_default" in result.stdout81 assert "Second: Morty" in result.stdout
modified:typer/rich_utils.py+5 added-3 removed
286 unmodified lines
287288 # Default value289 # This uses Typer's specific param._get_default_string290 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 ):296 default_str = param._get_default_string(297 ctx=ctx,298 show_default_is_str=show_default_is_str,299 default_value=default_value,
447 unmodified lines