Skip to content

Commit 5fd87bd

Browse files
authored
Merge pull request #2248 from jreese/8.1.x
Disallow use of is_flag and multiple in options
2 parents afdfb12 + daa2d8e commit 5fd87bd

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ Unreleased
77

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

1114

1215
Version 8.1.2

src/click/core.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2580,6 +2580,9 @@ def __init__(
25802580
if self.is_flag:
25812581
raise TypeError("'count' is not valid with 'is_flag'.")
25822582

2583+
if self.multiple and self.is_flag:
2584+
raise TypeError("'multiple' is not valid with 'is_flag', use 'count'.")
2585+
25832586
def to_info_dict(self) -> t.Dict[str, t.Any]:
25842587
info_dict = super().to_info_dict()
25852588
info_dict.update(

tests/test_options.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,3 +904,21 @@ def test_type_from_flag_value():
904904
)
905905
def test_is_bool_flag_is_correctly_set(option, expected):
906906
assert option.is_bool_flag is expected
907+
908+
909+
@pytest.mark.parametrize(
910+
("kwargs", "message"),
911+
[
912+
({"count": True, "multiple": True}, "'count' is not valid with 'multiple'."),
913+
({"count": True, "is_flag": True}, "'count' is not valid with 'is_flag'."),
914+
(
915+
{"multiple": True, "is_flag": True},
916+
"'multiple' is not valid with 'is_flag', use 'count'.",
917+
),
918+
],
919+
)
920+
def test_invalid_flag_combinations(runner, kwargs, message):
921+
with pytest.raises(TypeError) as e:
922+
click.Option(["-a"], **kwargs)
923+
924+
assert message in str(e.value)

0 commit comments

Comments
 (0)