From daa2d8e44332f66f09be83a9872218fde318bb8d Mon Sep 17 00:00:00 2001 From: John Reese Date: Tue, 5 Apr 2022 12:57:19 -0700 Subject: [PATCH] disallow use of is_flag and multiple in option --- CHANGES.rst | 3 +++ src/click/core.py | 3 +++ tests/test_options.py | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index a618f5a8c..16a982dd9 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 a9a72c5f9..5abfb0f3c 100644 --- a/src/click/core.py +++ b/src/click/core.py @@ -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( diff --git a/tests/test_options.py b/tests/test_options.py index 2e1f29106..d4090c725 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)