Skip to content

Commit

Permalink
Revert inconsistent choice metavar formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmups authored and AndreasBackx committed Nov 2, 2024
1 parent b2642d4 commit f7d98fe
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 17 deletions.
16 changes: 3 additions & 13 deletions src/click/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,23 +259,13 @@ def to_info_dict(self) -> dict[str, t.Any]:
return info_dict

def get_metavar(self, param: Parameter) -> str:
# Use choice ParamTypes if choices are hidden.
if param.param_type_name == "option" and not param.show_choices: # type: ignore
_choices = [
choice_metavars = [
convert_type(type(choice)).name.upper() for choice in self.choices
]
choices_str = "|".join([*dict.fromkeys(choice_metavars)])
else:
_choices = [str(i) for i in self.choices]

# Dedupe choices
_choices = [*dict.fromkeys(_choices)]

# Create choices_str
choices_str = "|".join(_choices)

# Use no braces if single choice
if len(_choices) < 2:
return choices_str
choices_str = "|".join([str(i) for i in self.choices])

# Use curly braces to indicate a required argument.
if param.required and param.param_type_name == "argument":
Expand Down
8 changes: 4 additions & 4 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,10 +931,10 @@ def test_invalid_flag_combinations(runner, kwargs, message):
@pytest.mark.parametrize(
("choices", "metavars"),
[
pytest.param(["foo", "bar"], "TEXT", id="text choices"),
pytest.param([1, 2], "INTEGER", id="int choices"),
pytest.param([1.0, 2.0], "FLOAT", id="float choices"),
pytest.param([True, False], "BOOLEAN", id="bool choices"),
pytest.param(["foo", "bar"], "[TEXT]", id="text choices"),
pytest.param([1, 2], "[INTEGER]", id="int choices"),
pytest.param([1.0, 2.0], "[FLOAT]", id="float choices"),
pytest.param([True, False], "[BOOLEAN]", id="bool choices"),
pytest.param(["foo", 1], "[TEXT|INTEGER]", id="text/int choices"),
],
)
Expand Down

0 comments on commit f7d98fe

Please sign in to comment.