Skip to content

Commit

Permalink
Merge pull request #2248 from jreese/8.1.x
Browse files Browse the repository at this point in the history
Disallow use of is_flag and multiple in options
  • Loading branch information
davidism authored Apr 28, 2022
2 parents afdfb12 + daa2d8e commit 5fd87bd
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 5fd87bd

Please # to comment.