Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide default value when show_default is False #2509

Merged
merged 5 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Hide default value when show_default is False
  • Loading branch information
pfhayes authored and AndreasBackx committed Nov 2, 2024
commit 48cc80f7d73eddf6f1ed094220afaecec30317d3
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Unreleased
- Show the ``types.ParamType.name`` for ``types.Choice`` options within
``--help`` message if ``show_choices=False`` is specified.
:issue:`2356`
- Do not display values in prompts when `show_default=False` on Options
AndreasBackx marked this conversation as resolved.
Show resolved Hide resolved


Version 8.1.8
Expand Down
1 change: 1 addition & 0 deletions src/click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2803,6 +2803,7 @@ def prompt_for_value(self, ctx: Context) -> t.Any:
show_choices=self.show_choices,
confirmation_prompt=self.confirmation_prompt,
value_proc=lambda x: self.process_value(ctx, x),
show_default=(False if self.show_default is False else True),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not simply: show_default=self.show_default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this class, self.show_default is a Union[bool, str, None], but termui.prompt accepts only bool. Passing through show_default directly gives a type error, and also appears to do the wrong things for values of type filenam

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Thanks for the explanation.

Would you mind writing it down as a comment in the code? I think it would help others who read this line of code for the first time.

)

def resolve_envvar_value(self, ctx: Context) -> str | None:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_termui.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,13 @@ def cli(password):

if prompt == "Confirm Password":
assert "Confirm Password: " in result.output


def test_show_default_false_hides_prompt(runner):
Copy link
Collaborator

@saroad2 saroad2 May 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test name can be clearer in order to explain what does this test check.

Mybe something like "test_false_show_default_cause_no_default_display_in_prompt".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@click.command()
@click.option("--arg1", show_default=False, prompt=True, default="my-default-value")
def cmd(arg1):
pass

result = runner.invoke(cmd, input="my-input", standalone_mode=False)
saroad2 marked this conversation as resolved.
Show resolved Hide resolved
assert "my-default-value" not in result.output