summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Reese <john@noswap.com>2022-04-05 12:57:19 -0700
committerDavid Lord <davidism@gmail.com>2022-04-28 06:51:25 -0700
commitdaa2d8e44332f66f09be83a9872218fde318bb8d (patch)
treee0b74bb01ddb36888e4abea73b62b0232862f11e
parentafdfb120fff5cb5f8d0184d411369f5dddaed5b3 (diff)
downloadclick-daa2d8e44332f66f09be83a9872218fde318bb8d.tar.gz
disallow use of is_flag and multiple in option
-rw-r--r--CHANGES.rst3
-rw-r--r--src/click/core.py3
-rw-r--r--tests/test_options.py18
3 files changed, 24 insertions, 0 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index a618f5a..16a982d 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -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
diff --git a/src/click/core.py b/src/click/core.py
index a9a72c5..5abfb0f 100644
--- a/src/click/core.py
+++ b/src/click/core.py
@@ -2580,6 +2580,9 @@ class Option(Parameter):
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(
diff --git a/tests/test_options.py b/tests/test_options.py
index 2e1f291..d4090c7 100644
--- a/tests/test_options.py
+++ b/tests/test_options.py
@@ -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)