Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Flags with multiple=True trigger TypeError #2246

Closed
amyreese opened this issue Apr 5, 2022 · 2 comments
Closed

Flags with multiple=True trigger TypeError #2246

amyreese opened this issue Apr 5, 2022 · 2 comments
Milestone

Comments

@amyreese
Copy link
Contributor

amyreese commented Apr 5, 2022

Adding an option with is_flag=True and multiple=True will trigger a TypeError in Parameter.consume_value() due to bad default value.

import click

@click.command()
@click.option("-v", is_flag=True, multiple=True)
def main(v):
    click.echo(v)

if __name__ == "__main__":
    main()

When run without the flag, a TypeError is raised:

(venv39) jreese@butterfree ~/scratch » python foo.py
Traceback (most recent call last):
  File "/Users/jreese/scratch/foo.py", line 25, in <module>
    main()
  File "/Users/jreese/scratch/venv39/lib/python3.9/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/Users/jreese/scratch/venv39/lib/python3.9/site-packages/click/core.py", line 1054, in main
    with self.make_context(prog_name, args, **extra) as ctx:
  File "/Users/jreese/scratch/venv39/lib/python3.9/site-packages/click/core.py", line 920, in make_context
    self.parse_args(ctx, args)
  File "/Users/jreese/scratch/venv39/lib/python3.9/site-packages/click/core.py", line 1378, in parse_args
    value, args = param.handle_parse_result(ctx, opts, args)
  File "/Users/jreese/scratch/venv39/lib/python3.9/site-packages/click/core.py", line 2356, in handle_parse_result
    value, source = self.consume_value(ctx, opts)
  File "/Users/jreese/scratch/venv39/lib/python3.9/site-packages/click/core.py", line 2903, in consume_value
    and any(v is _flag_needs_value for v in value)
TypeError: 'bool' object is not iterable

Manually setting a better default value, like default=(), avoids the type error, but this used to work on 7.x without setting the default value.

Environment:

  • Python version: tested on 3.8.6 and 3.9.3
  • Click version: worked on 7.1.2; errors on 8.0.4 and 8.1.2
@davidism davidism changed the title Regression: Flags with multiple=True trigger TypeError Flags with multiple=True trigger TypeError Apr 5, 2022
@davidism davidism added this to the 8.1.3 milestone Apr 5, 2022
amyreese added a commit to amyreese/click that referenced this issue Apr 5, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2022
@pallets pallets unlocked this conversation Jul 3, 2023
@davidism
Copy link
Member

davidism commented Jul 3, 2023

I can't remember what the original discussion about this was, but it seems like this should have been addressed by modifying the default default for multiple=True, flag=True rather than raising an error. The following works fine, the default=() should be implicit.

import click

@click.command
@click.option(
    "-y/-n",
    is_flag=True,
    multiple=True,
    default=()
)
def main(y):
    click.echo(y)


if __name__ == "__main__":
    main([])

@davidism davidism reopened this Jul 3, 2023
@davidism
Copy link
Member

davidism commented Jul 3, 2023

Issues #2292 and #2295 are due to the error added by the PR for this issue.

@davidism davidism closed this as completed Jul 3, 2023
fumiama pushed a commit to fumiama/packages that referenced this issue Jul 16, 2023
Version 8.1.3
Released 2022-04-28

  Use verbose form of typing.Callable for @command and @group.
  pallets/click#2255

  Show error when attempting to create an option with multiple=True,
  is_flag=True. Use count instead. pallets/click#2246

Version 8.1.2
Released 2022-03-31

  Fix error message for readable path check that was mixed up with the
  executable check. pallets/click#2236

  Restore parameter order for Path, placing the executable parameter at
  the end. It is recommended to use keyword arguments instead of
  positional arguments. pallets/click#2235

Version 8.1.1
Released 2022-03-30

  Fix an issue with decorator typing that caused type checking to
  report that a command was not callable. pallets/click#2227

Version 8.1.0
Released 2022-03-28

  Drop support for Python 3.6. pallets/click#2129

  Remove previously deprecated code. pallets/click#2130

    Group.resultcallback is renamed to result_callback.

    autocompletion parameter to Command is renamed to shell_complete.

    get_terminal_size is removed, use shutil.get_terminal_size instead.

    get_os_args is removed, use sys.argv[1:] instead.

  Rely on PEP 538 and PEP 540 to handle selecting UTF-8 encoding
  instead of ASCII. Click’s locale encoding detection is removed.
  pallets/click#2198

  Single options boolean flags with show_default=True only show the
  default if it is True. pallets/click#1971

  The command and group decorators can be applied with or without
  parentheses. pallets/click#1359

  The Path type can check whether the target is executable.
  pallets/click#1961

  Command.show_default overrides Context.show_default, instead of the
  other way around. pallets/click#1963

  Parameter decorators and @group handles cls=None the same as not
  passing cls. @option handles help=None the same as not passing help.
  pallets/click#1959

  A flag option with required=True requires that the flag is passed
  instead of choosing the implicit default value. pallets/click#1978

  Indentation in help text passed to Option and Command is cleaned the
  same as using the @option and @command decorators does. A command’s
  epilog and short_help are also processed. pallets/click#1985

  Store unprocessed Command.help, epilog and short_help strings.
  Processing is only done when formatting help text for output.
  pallets/click#2149

  Allow empty str input for prompt() when confirmation_prompt=True and
  default="". pallets/click#2157

  Windows glob pattern expansion doesn’t fail if a value is an invalid
  pattern. pallets/click#2195

  It’s possible to pass a list of params to @command. Any params
  defined with decorators are appended to the passed params.
  pallets/click#2131

  @command decorator is annotated as returning the correct type if a
  cls argument is used. pallets/click#2211

  A Group with invoke_without_command=True and chain=False will invoke
  its result callback with the group function’s return value.
  pallets/click#2124

  to_info_dict will not fail if a ParamType doesn’t define a name.
  pallets/click#2168

  Shell completion prioritizes option values with option prefixes over
  new options. pallets/click#2040

  Options that get an environment variable value using
  autoenvvar_prefix treat an empty value as None, consistent with a
  direct envvar. pallets/click#2146

Version 8.0.4
Released 2022-02-18

  open_file recognizes Path("-") as a standard stream, the same as the
  string "-". pallets/click#2106

  The option and argument decorators preserve the type annotation of
  the decorated function. pallets/click#2155

  A callable default value can customize its help text by overriding
  __str__ instead of always showing (dynamic). pallets/click#2099

  Fix a typo in the Bash completion script that affected file and
  directory completion. If this script was generated by a previous
  version, it should be regenerated. pallets/click#2163

  Fix typing for echo and secho file argument. pallets/click#2174,
  pallets/click#2185

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants