Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow use of is_flag and multiple in options #2248

Merged
merged 1 commit into from Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
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
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
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)