Skip to content

Commit f46d049

Browse files
committed
Rebase and fix tests.
1 parent c1ee9c1 commit f46d049

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

src/click/types.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -354,16 +354,12 @@ def get_invalid_choice_message(self, value: t.Any, ctx: Context | None) -> str:
354354
355355
.. versionadded:: 8.2
356356
"""
357-
choices_str = ", ".join(self.normalized_mapping(ctx=ctx).values())
358-
self.fail(
359-
message=ngettext(
360-
"{value!r} is not {choice}.",
361-
"{value!r} is not one of {choices}.",
362-
len(self.choices),
363-
).format(value=value, choice=choices_str, choices=choices_str),
364-
param=value,
365-
ctx=ctx,
366-
)
357+
choices_str = ", ".join(map(repr, self.normalized_mapping(ctx=ctx).values()))
358+
return ngettext(
359+
"{value!r} is not {choice}.",
360+
"{value!r} is not one of {choices}.",
361+
len(self.choices),
362+
).format(value=value, choice=choices_str, choices=choices_str)
367363

368364
def __repr__(self) -> str:
369365
return f"Choice({list(self.choices)})"

tests/test_basic.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -387,25 +387,34 @@ def test_choice_option_normalization(runner):
387387
@click.option(
388388
"--method",
389389
type=click.Choice(
390-
["SCREAMING_SNAKE_CASE", "snake_case", "PascalCase", "kebab-case"]
390+
["SCREAMING_SNAKE_CASE", "snake_case", "PascalCase", "kebab-case"],
391+
case_sensitive=False,
391392
),
392393
)
393394
def cli(method):
394395
click.echo(method)
395396

396-
result = runner.invoke(cli, ["--method=foo"])
397-
assert not result.exception
398-
assert result.output == "foo\n"
397+
result = runner.invoke(cli, ["--method=snake_case"])
398+
assert not result.exception, result.output
399+
assert result.output == "snake_case\n"
400+
401+
# Even though it's case sensitive, the choice's original value is preserved
402+
result = runner.invoke(cli, ["--method=pascalcase"])
403+
assert not result.exception, result.output
404+
assert result.output == "PascalCase\n"
399405

400406
result = runner.invoke(cli, ["--method=meh"])
401407
assert result.exit_code == 2
402408
assert (
403409
"Invalid value for '--method': 'meh' is not one of "
404-
"'screaming-snake-case', 'snake-case', 'pascalcase', 'kebab-case'."
410+
"'screaming_snake_case', 'snake_case', 'pascalcase', 'kebab-case'."
405411
) in result.output
406412

407413
result = runner.invoke(cli, ["--help"])
408-
assert "--method [foo|bar|baz]" in result.output
414+
assert (
415+
"--method [screaming_snake_case|snake_case|pascalcase|kebab-case]"
416+
in result.output
417+
)
409418

410419

411420
def test_choice_argument(runner):

tests/test_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,5 +248,5 @@ def test_invalid_path_with_esc_sequence():
248248

249249
def test_choice_get_invalid_choice_message():
250250
choice = click.Choice(["a", "b", "c"])
251-
message = choice.get_invalid_choice_message("d")
251+
message = choice.get_invalid_choice_message("d", ctx=None)
252252
assert message == "'d' is not one of 'a', 'b', 'c'."

0 commit comments

Comments
 (0)