Skip to content

Commit

Permalink
Disallow use of is_flag and multiple in options
Browse files Browse the repository at this point in the history
  • Loading branch information
amyreese committed Apr 5, 2022
1 parent 2f8a341 commit 9c1b957
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/click/core.py
Expand Up @@ -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(
Expand Down
28 changes: 28 additions & 0 deletions tests/test_options.py
Expand Up @@ -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")

0 comments on commit 9c1b957

Please sign in to comment.