Skip to content
Myr

fix: show custom error message in prompt with hide_input=True

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by doiken · committed by Kevin Deldycke · parent fc6c7c4 · Browse files · 3 files+73 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+73 added−1 removed

  • tests:+55 added−0 removed
  • other:+18 added−1 removed

3 files+73 added−1 removed

modified:src/click/termui.py+14 added-1 removed
201 unmodified lines
202 try:203 result = value_proc(value)204 except UsageError as e:205 if hide_input:206 echo(_("Error: The value you entered was invalid."), err=err)206 repr_val = repr(value)207 if repr_val in e.message:208 # Built-in type pattern: mask the repr'd value.209 msg = e.message.replace(repr_val, "'***'")210 elif value in e.message:211 # Raw value found: could be a coincidental or212 # unquoted match. Ambiguous, use generic.213 msg = _("The value you entered was invalid.")214 else:215 # Value not found: show as-is, assuming custom216 # types with hide_input=True avoid leaking input.217 msg = e.message218219 echo(_("Error: {msg}").format(msg=msg), err=err)220 else:221 echo(_("Error: {e.message}").format(e=e), err=err)222 continue223 if not confirmation_prompt:
711 unmodified lines
modified:tests/test_termui.py+55 added
986 unmodified lines
987988 assert result.output == expected_output989 assert not result.stderr990 assert result.exit_code == 0 if expected not in (REPEAT, INVALID) else 1991992993class _CustomTypeNoValue(click.ParamType):994 name = "custom"995996 def convert(self, value, param, ctx):997 if len(value) < 4:998 self.fail("Password must be at least 4 characters", param, ctx)999 return value100010011002class _CustomTypeWithRawValue(click.ParamType):1003 name = "custom_raw"10041005 def convert(self, value, param, ctx):1006 if value == "bad":1007 self.fail(f"rejected: {value}", param, ctx)1008 return value100910101011@pytest.mark.parametrize(1012 ("type", "expected_fragment", "unexpected_fragment"),1013 [1014 pytest.param(1015 click.INT,1016 "'***' is not a valid integer",1017 "bad",1018 id="builtin-int-masks-repr-value",1019 ),1020 pytest.param(1021 _CustomTypeNoValue(),1022 "Password must be at least 4 characters",1023 None,1024 id="custom-no-value-shows-message",1025 ),1026 pytest.param(1027 _CustomTypeWithRawValue(),1028 "The value you entered was invalid",1029 "bad",1030 id="custom-raw-value-falls-back-to-generic",1031 ),1032 ],1033)1034def test_hide_input_error_message(runner, type, expected_fragment, unexpected_fragment):1035 """https://github.com/pallets/click/issues/2809"""10361037 @click.command()1038 @click.option("--password", prompt=True, hide_input=True, type=type)1039 def cli(password):1040 click.echo(password)10411042 result = runner.invoke(cli, input="bad")1043 assert expected_fragment in result.output1044 if unexpected_fragment is not None:1045 assert unexpected_fragment not in result.output
modified:CHANGES.rst+4 added
51 unmodified lines
52- Use :class:`ValueError` message when conversion in :class:`FuncParamType` would53 fail. :issue:`3105` :pr:`3211`54- Add ``click.get_pager_file`` for file-like access to an output55 pager. :pr:`1572`56- Show custom error messages from types when :func:`prompt` with57 ``hide_input=True`` fails validation, instead of always showing a58 generic message. Built-in type messages mask the input value.59 :issue:`2809` :pr:`3256`6061Version 8.3.362-------------63
1364 unmodified lines