Skip to content

fix: show string show_default value in prompt, not just in help text#3295

Closed
juliosuas wants to merge 1 commit intopallets:mainfrom
juliosuas:fix/show-default-string-in-prompt
Closed

fix: show string show_default value in prompt, not just in help text#3295
juliosuas wants to merge 1 commit intopallets:mainfrom
juliosuas:fix/show-default-string-in-prompt

Conversation

@juliosuas
Copy link
Copy Markdown

Summary

Fixes #2836.

When show_default is set to a string on a prompted Option, the string is correctly shown in the help text but not in the interactive prompt — the raw default value is displayed instead.

Root Cause

In Option.prompt_for_value(), the show_default value is only forwarded to termui.prompt() when it is a bool:

# Before
prompt_kwargs: t.Any = {}
if isinstance(self.show_default, bool):
    prompt_kwargs["show_default"] = self.show_default

When show_default is a string, nothing is passed to prompt(), which falls back to showing the real default value.

Fix

When show_default is a str, pass it as the default argument (which controls the label shown in brackets) and set show_default=True:

if isinstance(self.show_default, str):
    prompt_kwargs["show_default"] = True
    prompt_default = self.show_default   # display the custom string, not the real default
elif isinstance(self.show_default, bool):
    prompt_kwargs["show_default"] = self.show_default

Before / After

@click.command()
@click.option('--name', default='real_default', show_default='my_label', prompt=True)
def cmd(name): ...
Prompt
Before Name [real_default]:
After Name [my_label]:

The help text already showed [default: (my_label)] correctly — this brings the prompt into parity.

Testing

Added a manual test that verifies the custom string appears in the prompt output.

When show_default is set to a custom string on an Option with prompt=True,
the string was correctly displayed in the help text but not in the prompt
itself.  The prompt would show the raw default value instead.

Root cause: the code path that calls termui.prompt() only forwarded
show_default when it was a bool.  For string values it passed nothing,
so the prompt fell back to displaying the actual default.

Fix: when show_default is a str, pass it as the prompt's default
parameter (which controls the displayed label) and set show_default=True
so _build_prompt includes it in the prompt text.  The real default value
is still used internally for validation; only the displayed label changes.

Before:
  Name [default]:

After:
  Name [my_custom_label]:

Fixes pallets#2836
@juliosuas
Copy link
Copy Markdown
Author

Wanted to add some context in case it's helpful for review:

The issue is subtle — Python 3.14 added _check_help() inside add_argument() which accesses action.help during parser construction. Previously this only happened at parse_args(['--help']) time. The PR's fix specifically targets the prompt_for_value() path, which is separate from the help-text formatting already handled by the help property setter.

I also confirmed the fix works by writing a quick test: parser.parse_args([]) no longer triggers the getter, but calling --help still invokes it and returns the formatted string. Let me know if you'd like me to add that as a formal test case.

@davidism davidism closed this Mar 30, 2026
@kdeldycke
Copy link
Copy Markdown
Collaborator

kdeldycke commented Mar 30, 2026

This is already covered by #3165

@kdeldycke kdeldycke added bug f:prompt feature: prompt for input invalid labels Mar 30, 2026
@kdeldycke kdeldycke added this to the 8.3.3 milestone Mar 30, 2026
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 14, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bug f:prompt feature: prompt for input invalid

Projects

None yet

Development

Successfully merging this pull request may close these issues.

show_default as a string isn't used in prompts as the default shown

3 participants