diff options
author | David Lord <davidism@gmail.com> | 2022-04-28 06:55:44 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-28 06:55:44 -0700 |
commit | 5fd87bdf80ed450334b37344f6c99890c217d3db (patch) | |
tree | e0b74bb01ddb36888e4abea73b62b0232862f11e | |
parent | afdfb120fff5cb5f8d0184d411369f5dddaed5b3 (diff) | |
parent | daa2d8e44332f66f09be83a9872218fde318bb8d (diff) | |
download | click-5fd87bdf80ed450334b37344f6c99890c217d3db.tar.gz |
Merge pull request #2248 from jreese/8.1.x
Disallow use of is_flag and multiple in options
-rw-r--r-- | CHANGES.rst | 3 | ||||
-rw-r--r-- | src/click/core.py | 3 | ||||
-rw-r--r-- | tests/test_options.py | 18 |
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) |