typer icon indicating copy to clipboard operation
typer copied to clipboard

Add callable support to `show_default`

Open zanieb opened this issue 2 years ago • 4 comments

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.

zanieb avatar Feb 12 '22 21:02 zanieb

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

Dymstro avatar Apr 07 '22 15:04 Dymstro

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.

zanieb avatar Jun 06 '22 22:06 zanieb

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.

zanieb avatar Sep 08 '22 21:09 zanieb

[maintenance note] I'd suggest reviewing https://github.com/tiangolo/typer/pull/501 first and then updating this PR to review the added feature.

svlandeg avatar Mar 08 '24 14:03 svlandeg