Skip to content
Myr

feat: new method for choice fail (#2622)

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

authored by antazoey · committed by GitHub · parent 1787497 · Browse files · 4 files+23 added−9 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

4 files+23 added−9 removed

  • tests:+6 added−0 removed
  • other:+17 added−9 removed

4 files+23 added−9 removed

modified:docs/api.rst+1 added
132 unmodified lines
133134.. autoclass:: Path135136.. autoclass:: Choice137 :members:138139.. autoclass:: IntRange140141.. autoclass:: FloatRange
69 unmodified lines
modified:src/click/types.py+14 added-9 removed
302 unmodified lines
303304 if normed_value in normed_choices:305 return normed_choices[normed_value]306307 self.fail(self.get_invalid_choice_message(value), param, ctx)308309 def get_invalid_choice_message(self, value: t.Any) -> str:310 """Get the error message when the given choice is invalid.311312 :param value: The invalid value.313314 .. versionadded:: 8.2315 """316 choices_str = ", ".join(map(repr, self.choices))308 self.fail(309 ngettext(310 "{value!r} is not {choice}.",311 "{value!r} is not one of {choices}.",312 len(self.choices),313 ).format(value=value, choice=choices_str, choices=choices_str),317 return ngettext(318 "{value!r} is not {choice}.",319 "{value!r} is not one of {choices}.",320 len(self.choices),321 ).format(value=value, choice=choices_str, choices=choices_str)314 param,315 ctx,316 )322323 def __repr__(self) -> str:324 return f"Choice({list(self.choices)})"325
784 unmodified lines
modified:tests/test_types.py+6 added
242 unmodified lines
243 with tempfile.TemporaryDirectory(prefix="my\ndir") as tempdir:244 click.Path(dir_okay=False).convert(tempdir, None, None)245246 assert "my\\ndir" in exc_info.value.message247248249def test_choice_get_invalid_choice_message():250 choice = click.Choice(["a", "b", "c"])251 message = choice.get_invalid_choice_message("d")252 assert message == "'d' is not one of 'a', 'b', 'c'."
modified:CHANGES.rst+2 added
52 unmodified lines
53 same name. :issue:`2396``54- When using ``Option.envvar`` with ``Option.flag_value``, the ``flag_value``55 will always be used instead of the value of the environment variable.56 :issue:`2746` :pr:`2788`57- Add ``Choice.get_invalid_choice_message`` method for customizing the58 invalid choice message. :issue:`2621` :pr:`2622`596061Version 8.1.862-------------
1116 unmodified lines