From 9c1b95772e9914eef89bfa1b9f09a15ad6c909a2 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 options --- src/click/core.py | 4 ++++ tests/test_options.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/click/core.py b/src/click/core.py index a9a72c5f9..e43078acb 100644 --- a/src/click/core.py +++ b/src/click/core.py @@ -2580,6 +2580,10 @@ def __init__( if self.is_flag: raise TypeError("'count' is not valid with 'is_flag'.") + if self.multiple: + if self.is_flag: + raise TypeError("'multiple' is not valid with 'is_flag'.") + 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..fb5d6129c 100644 --- a/tests/test_options.py +++ b/tests/test_options.py @@ -904,3 +904,31 @@ def test_type_from_flag_value(): ) def test_is_bool_flag_is_correctly_set(option, expected): assert option.is_bool_flag is expected + + +def test_invalid_flag_combinations(runner): + try: + + @click.command() + @click.option("--foo", is_flag=True, count=True) + def cli1(foo): + pass + + except TypeError as e: + assert "'count' is not valid with 'is_flag'" in str(e) + + else: + raise AssertionError("didn't raise TypeError for use of count and is_flag") + + try: + + @click.command() + @click.option("--foo", is_flag=True, multiple=True) + def cli2(foo): + pass + + except TypeError as e: + assert "'multiple' is not valid with 'is_flag'" in str(e) + + else: + raise AssertionError("didn't raise TypeError for use of multiple and is_flag")