Skip to content

Commit

Permalink
disallow use of is_flag and multiple in option
Browse files Browse the repository at this point in the history
  • Loading branch information
amyreese authored and davidism committed Apr 28, 2022
1 parent afdfb12 commit daa2d8e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Unreleased

- Use verbose form of ``typing.Callable`` for ``@command`` and
``@group``. :issue:`2255`
- Show error when attempting to create an option with
``multiple=True, is_flag=True``. Use ``count`` instead.
:issue:`2246`


Version 8.1.2
Expand Down
3 changes: 3 additions & 0 deletions src/click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2580,6 +2580,9 @@ def __init__(
if self.is_flag:
raise TypeError("'count' is not valid with 'is_flag'.")

if self.multiple and self.is_flag:
raise TypeError("'multiple' is not valid with 'is_flag', use 'count'.")

def to_info_dict(self) -> t.Dict[str, t.Any]:
info_dict = super().to_info_dict()
info_dict.update(
Expand Down
18 changes: 18 additions & 0 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,3 +904,21 @@ def test_type_from_flag_value():
)
def test_is_bool_flag_is_correctly_set(option, expected):
assert option.is_bool_flag is expected


@pytest.mark.parametrize(
("kwargs", "message"),
[
({"count": True, "multiple": True}, "'count' is not valid with 'multiple'."),
({"count": True, "is_flag": True}, "'count' is not valid with 'is_flag'."),
(
{"multiple": True, "is_flag": True},
"'multiple' is not valid with 'is_flag', use 'count'.",
),
],
)
def test_invalid_flag_combinations(runner, kwargs, message):
with pytest.raises(TypeError) as e:
click.Option(["-a"], **kwargs)

assert message in str(e.value)

0 comments on commit daa2d8e

Please sign in to comment.