click icon indicating copy to clipboard operation
click copied to clipboard

Multi-value flags broken by recent change

Open hashstat opened this issue 3 years ago • 2 comments

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

hashstat avatar May 24 '22 17:05 hashstat

Happy to review a PR

davidism avatar May 24 '22 17:05 davidism

Was this fixed?

marcosfelt avatar Aug 17 '22 11:08 marcosfelt

@marcosfelt Fix exists in PR #2293 by @hashstat but has not been merged.

@davidism could you review? It's really concise PR, just being more specific in raising an exception. PR also diligently adds test case for the feature.

epruesse avatar Oct 26 '22 22:10 epruesse