Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Unreleased

- Show custom error messages from types when ``prompt`` with
``hide_input=True`` fails validation, instead of always showing a
generic message. Built-in type messages mask the input value.
:issue:`2809`
- Fix handling of ``flag_value`` when ``is_flag=False`` to allow such options to be
used without an explicit value. :issue:`3084`
- Hide ``Sentinel.UNSET`` values as ``None`` when using ``lookup_default()``.
Expand Down
15 changes: 14 additions & 1 deletion src/click/termui.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,20 @@ def prompt_func(text: str) -> str:
result = value_proc(value)
except UsageError as e:
if hide_input:
echo(_("Error: The value you entered was invalid."), err=err)
repr_val = repr(value)
if repr_val in e.message:
# Built-in type pattern: mask the repr'd value.
msg = e.message.replace(repr_val, "'***'")
elif value in e.message:
# Raw value found: could be a coincidental or
# unquoted match. Ambiguous, use generic.
msg = _("The value you entered was invalid.")
else:
# Value not found: show as-is, assuming custom
# types with hide_input=True avoid leaking input.
msg = e.message

echo(_("Error: {msg}").format(msg=msg), err=err)
else:
echo(_("Error: {e.message}").format(e=e), err=err)
continue
Expand Down
55 changes: 55 additions & 0 deletions tests/test_termui.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,3 +710,58 @@ def cli(flag):
assert result.output == expected_output
assert not result.stderr
assert result.exit_code == 0 if expected not in (REPEAT, INVALID) else 1


class _CustomTypeNoValue(click.ParamType):
name = "custom"

def convert(self, value, param, ctx):
if len(value) < 4:
self.fail("Password must be at least 4 characters", param, ctx)
return value


class _CustomTypeWithRawValue(click.ParamType):
name = "custom_raw"

def convert(self, value, param, ctx):
if value == "bad":
self.fail(f"rejected: {value}", param, ctx)
return value


@pytest.mark.parametrize(
("type", "expected_fragment", "unexpected_fragment"),
[
pytest.param(
click.INT,
"'***' is not a valid integer",
"bad",
id="builtin-int-masks-repr-value",
),
pytest.param(
_CustomTypeNoValue(),
"Password must be at least 4 characters",
None,
id="custom-no-value-shows-message",
),
pytest.param(
_CustomTypeWithRawValue(),
"The value you entered was invalid",
"bad",
id="custom-raw-value-falls-back-to-generic",
),
],
)
def test_hide_input_error_message(runner, type, expected_fragment, unexpected_fragment):
"""https://github.com/pallets/click/issues/2809"""

@click.command()
@click.option("--password", prompt=True, hide_input=True, type=type)
def cli(password):
click.echo(password)

result = runner.invoke(cli, input="bad")
assert expected_fragment in result.output
if unexpected_fragment is not None:
assert unexpected_fragment not in result.output
Loading