iterative / shtab

↔️ Automagic shell tab completion for Python CLI applications
https://docs.iterative.ai/shtab
Other
363 stars 35 forks source link

[bug] TypeError: 'dict_values' object is not subscriptable #95

Closed Freed-Wu closed 1 year ago

Freed-Wu commented 2 years ago

I try to generate completes for tensorboard https://github.com/tensorflow/tensorboard/pull/5902, only zsh failed:

❯ tensorboard --print-completion zsh
TensorFlow installation not found - running with reduced feature set.
Traceback (most recent call last):
  File "/usr/bin/tensorboard", line 33, in <module>
    sys.exit(load_entry_point('tensorboard==2.9.1', 'console_scripts', 'tensorboard')())
  File "/usr/lib/python3.10/site-packages/tensorboard/main.py", line 46, in run_main
    app.run(tensorboard.main, flags_parser=tensorboard.configure)
  File "/usr/lib/python3.10/site-packages/absl/app.py", line 304, in run
    args = _run_init(
  File "/usr/lib/python3.10/site-packages/absl/app.py", line 373, in _run_init
    args = _register_and_parse_flags_with_usage(
  File "/usr/lib/python3.10/site-packages/absl/app.py", line 220, in _register_and_parse_flags_with_usage
    args_to_main = flags_parser(original_argv)
  File "/usr/lib/python3.10/site-packages/tensorboard/program.py", line 236, in configure
    flags = base_parser.parse_args(argv[1:])  # Strip binary name from argv.
  File "/usr/lib/python3.10/argparse.py", line 1826, in parse_args
    args, argv = self.parse_known_args(args, namespace)
  File "/usr/lib/python3.10/site-packages/absl/flags/argparse_flags.py", line 168, in parse_known_args
    namespace, args = super(ArgumentParser, self).parse_known_args(
  File "/usr/lib/python3.10/argparse.py", line 1859, in parse_known_args
    namespace, args = self._parse_known_args(args, namespace)
  File "/usr/lib/python3.10/argparse.py", line 2068, in _parse_known_args
    start_index = consume_optional(start_index)
  File "/usr/lib/python3.10/argparse.py", line 2008, in consume_optional
    take_action(action, args, option_string)
  File "/usr/lib/python3.10/argparse.py", line 1936, in take_action
    action(self, namespace, argument_values, option_string)
  File "/home/wzy/.local/lib/python3.10/site-packages/shtab/__init__.py", line 787, in __call__
    print(complete(parent or parser, values, preamble=preamble))
  File "/home/wzy/.local/lib/python3.10/site-packages/shtab/__init__.py", line 776, in complete
    return completer(
  File "/home/wzy/.local/lib/python3.10/site-packages/shtab/__init__.py", line 509, in complete_zsh
    "cmd": prog, "arguments": [
  File "/home/wzy/.local/lib/python3.10/site-packages/shtab/__init__.py", line 510, in <listcomp>
    format_optional(opt)
  File "/home/wzy/.local/lib/python3.10/site-packages/shtab/__init__.py", line 492, in format_optional
    (choice_type2fn[opt.choices[0].type] if isinstance(opt.choices[0], Choice) else
TypeError: 'dict_values' object is not subscriptable

From command line, it also failed.

Thanks.

Simon-1-1 commented 1 year ago

Are you using the choices= parameter of add_argument? If so, make sure you are providing a tuple. I provided a list in my case, which resulted in this error.

Freed-Wu commented 1 year ago

OK, good suggestion. However, it still is a bug which need to be fixed :bug:.

casperdcl commented 1 year ago

As per https://docs.python.org/3/library/argparse.html#choices it seems any sequence should be supported. I don't think that includes dict (assert not isinstance(dict, typing.Sequence))

kshitiz305 commented 1 year ago

So what shall be the fix be ?