Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Multi-value flags broken by recent change #2292

Closed
hashstat opened this issue May 24, 2022 · 3 comments
Closed

Multi-value flags broken by recent change #2292

hashstat opened this issue May 24, 2022 · 3 comments
Milestone

Comments

@hashstat
Copy link

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
@davidism
Copy link
Member

Happy to review a PR

hashstat pushed a commit to hashstat/click that referenced this issue May 24, 2022
Allows creating options with is_flag=True and multiple=True if
flag_value is also set to a non-bool value.

See pallets#2292
hashstat pushed a commit to hashstat/click that referenced this issue May 24, 2022
Allows creating options with is_flag=True and multiple=True if
flag_value is also set to a non-bool value.

Fixes pallets#2292
hashstat pushed a commit to hashstat/click that referenced this issue May 24, 2022
Allow creating options with is_flag=True and multiple=True if
flag_value is also set to a non-bool value.

Fixes pallets#2292
@marcosfelt
Copy link

Was this fixed?

marcosfelt added a commit to sustainable-processes/multitask that referenced this issue Aug 17, 2022
@epruesse
Copy link

@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.

hashstat pushed a commit to hashstat/click that referenced this issue Oct 27, 2022
Allow creating options with is_flag=True and multiple=True if
flag_value is also set to a non-bool value.

Fixes pallets#2292
hashstat pushed a commit to hashstat/click that referenced this issue Jan 26, 2023
Allow creating options with is_flag=True and multiple=True if
flag_value is also set to a non-bool value.

Fixes pallets#2292
@davidism davidism added this to the 8.1.4 milestone Jul 1, 2023
@davidism davidism linked a pull request Jul 1, 2023 that will close this issue
6 tasks
@davidism davidism closed this as completed Jul 3, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 18, 2023
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants