typer
typer copied to clipboard
Add callable support to `show_default`
Adds callable support to show_default
, allowing a Callable[[], Union[bool, str]]
to be passed to dynamically determine the default to show in help messages. Closes https://github.com/tiangolo/typer/issues/354.
Previously, show_default
was incorrectly typed as just bool
instead of Union[bool, str]
on Option
/ OptionType
. This fixes those type annotations. Closes #158.
Additionally, the default string for dynamic options was defined as default_string = _("(dynamic)")
which appears to be a bug and is confusing syntax if valid. This was simplified to match the line from arguments.
I've also removed the formatted parenthesis from the displayed show_default
value, as this value should be able to match the style of a real default. This is the only "breaking" change here.
I'll happily extend documentation / tests if you're willing to accept this.
Has anyone looked at this yet?
Having to add "# type: ignore[arg-type]" everytime I use show_default with a string in an option is getting annoying
Note this not only fixes the type hint for show_default
but also adds callable support to show_default
which is not just a type hint change. Just want to clarify since the label is a bit more narrow.
This conflicts with the refactor of defaults for rich_utils
. Unfortunately it does not appear straight-forward to reincorporate this improvement. Given that I haven't gotten a response yet, I won't attempt to do so yet. If you're willing to accept this feature enhancement please let me know and I will attempt to resolve the conflicts.
[maintenance note] I'd suggest reviewing https://github.com/tiangolo/typer/pull/501 first and then updating this PR to review the added feature.