Skip to content
Myr

`FuncParamType` should use `ValueError` message in `self.fail()` (#3211)

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

authored by STEPHEN OTIENO ONYANGO · committed by GitHub · parent 5b9630f · Browse files · 3 files+30 added−6 removed
This commit

State

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

Trailers

  • Co-authored-by: Andreas Backx <andreas@backx.org>

Diff stats

3 files+30 added−6 removed

  • tests:+19 added−0 removed
  • other:+11 added−6 removed

3 files+30 added−6 removed

modified:src/click/types.py+9 added-6 removed
193 unmodified lines
194 self, value: t.Any, param: Parameter | None, ctx: Context | None195 ) -> ParamTypeValue:196 try:197 return self.func(value)198 except ValueError:199 try:200 value = str(value)201 except UnicodeError:202 value = value.decode("utf-8", "replace")198 except ValueError as exc:199 message = str(exc)200204 self.fail(value, param, ctx)201 if not message:202 try:203 message = str(value)204 except UnicodeError:205 message = value.decode("utf-8", "replace")206207 self.fail(message, param, ctx)208209210class UnprocessedParamType(ParamType[t.Any]):211 name = "text"212213 def convert(
1086 unmodified lines
modified:tests/test_types.py+19 added
49 unmodified lines
5051 assert expect in exc_info.value.message525354@pytest.mark.parametrize(55 ("error_message", "expected"),56 [57 ("bad value: nope", "bad value: nope"),58 ("", "nope"),59 ],60)61def test_func_param_type_uses_value_error_message(error_message, expected):62 def parse(value):63 raise ValueError(error_message if error_message else "")6465 func_type = click.types.FuncParamType(parse)6667 with pytest.raises(click.BadParameter) as exc_info:68 func_type.convert("nope", None, None)6970 assert expected in exc_info.value.message717273def test_float_range_no_clamp_open():74 with pytest.raises(TypeError):75 click.FloatRange(0, 1, max_open=True, clamp=True)76
200 unmodified lines
modified:CHANGES.rst+2 added
47 unmodified lines
48- Fix Fish shell completion errors when option help text contains newlines.49 :issue:`3043`50- Add :class:`NoSuchCommand` exception with suggestions for misspelled51 commands. :issue:`3107` :pr:`3228`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`5657Version 8.3.3
1366 unmodified lines