Skip to content

Commit

Permalink
Rebase and fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasBackx committed Nov 9, 2024
1 parent 3167037 commit 6a03b04
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
16 changes: 6 additions & 10 deletions src/click/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,16 +354,12 @@ def get_invalid_choice_message(self, value: t.Any, ctx: Context | None) -> str:
.. versionadded:: 8.2
"""
choices_str = ", ".join(self.normalized_mapping(ctx=ctx).values())
self.fail(
message=ngettext(
"{value!r} is not {choice}.",
"{value!r} is not one of {choices}.",
len(self.choices),
).format(value=value, choice=choices_str, choices=choices_str),
param=value,
ctx=ctx,
)
choices_str = ", ".join(map(repr, self.normalized_mapping(ctx=ctx).values()))
return ngettext(
"{value!r} is not {choice}.",
"{value!r} is not one of {choices}.",
len(self.choices),
).format(value=value, choice=choices_str, choices=choices_str)

def __repr__(self) -> str:
return f"Choice({list(self.choices)})"
Expand Down
21 changes: 15 additions & 6 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,25 +387,34 @@ def test_choice_option_normalization(runner):
@click.option(
"--method",
type=click.Choice(
["SCREAMING_SNAKE_CASE", "snake_case", "PascalCase", "kebab-case"]
["SCREAMING_SNAKE_CASE", "snake_case", "PascalCase", "kebab-case"],
case_sensitive=False,
),
)
def cli(method):
click.echo(method)

result = runner.invoke(cli, ["--method=foo"])
assert not result.exception
assert result.output == "foo\n"
result = runner.invoke(cli, ["--method=snake_case"])
assert not result.exception, result.output
assert result.output == "snake_case\n"

# Even though it's case sensitive, the choice's original value is preserved
result = runner.invoke(cli, ["--method=pascalcase"])
assert not result.exception, result.output
assert result.output == "PascalCase\n"

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

result = runner.invoke(cli, ["--help"])
assert "--method [foo|bar|baz]" in result.output
assert (
"--method [screaming_snake_case|snake_case|pascalcase|kebab-case]"
in result.output
)


def test_choice_argument(runner):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,5 +248,5 @@ def test_invalid_path_with_esc_sequence():

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

0 comments on commit 6a03b04

Please sign in to comment.