Skip to content

Multi-value flags broken by recent change #2292

@hashstat

Description

@hashstat

A recent change (#2248), introduced in version 8.1.3, breaks a feature that allowed flag options to be used as aliases for multi-value options.

Here's an example that demonstrates two uses of the feature:

# feature.py
import click

@click.command
@click.option('--bar', 'tags', flag_value='bar', multiple=True, help='Alias for `--tag bar`.')
@click.option('--baz', 'tags', flag_value='baz', multiple=True, help='Alias for `--tag baz`.')
@click.option('--foo', 'tags', flag_value='foo', multiple=True, help='Alias for `--tag foo`.')
@click.option('-m', '--tag', 'tags', type=click.Choice(['foo', 'bar', 'baz']), multiple=True)
@click.option('-q', '--quiet', 'verbosity', is_flag=True, flag_value=-1, multiple=True)
@click.option('-v', '--verbose', 'verbosity', is_flag=True, flag_value=1, multiple=True)
def main(tags: tuple[str, ...], verbosity: tuple[int, ...]) -> None:
    print(f'tags = {tags}')
    print(f'verbosity = {sum(verbosity)}')

if __name__ == '__main__':
    main()

As of version 8.1.3, a TypeError is raised:

  File "feature.py", line 11, in <module>
    def main(tags: tuple[str, ...], verbosity: tuple[int, ...]) -> None:
  File "venv/lib/python3.9/site-packages/click/decorators.py", line 308, in decorator
    _param_memo(f, OptionClass(param_decls, **option_attrs))
  File "venv/lib/python3.9/site-packages/click/core.py", line 2584, in __init__
    raise TypeError("'multiple' is not valid with 'is_flag', use 'count'.")
TypeError: 'multiple' is not valid with 'is_flag', use 'count'.

But it works great in version 8.1.2 and earlier:

$ python3 feature.py  --tag bar --foo --baz -vvvq
tags = ('bar', 'foo', 'baz')
verbosity = 2

Environment:

  • Python version: 3.9.12
  • Click version: 8.1.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions