iterative / shtab

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

[bug] when choices is a set #155

Closed Freed-Wu closed 9 months ago

Freed-Wu commented 10 months ago

If we

    parser.add_argument(
        "--color",
        choices={"auto", "always", "never"},
        default="auto",
        help="when to display color",
    )

It is legal, because argparse only check choice in choices, and it will be faster than choices is a list.

However, shtab will:

        File "/tmp/pip-build-env-xrs7p_m9/overlay/lib/python3.11/site-packages/shtab/__init__.py", line 795, in __call__
          print(complete(parent or parser, values, preamble=preamble))
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/tmp/pip-build-env-xrs7p_m9/overlay/lib/python3.11/site-packages/shtab/__init__.py", line 784, in complete
          return completer(
                 ^^^^^^^^^^
        File "/tmp/pip-build-env-xrs7p_m9/overlay/lib/python3.11/site-packages/shtab/__init__.py", line 506, in complete_zsh
          "cmd": prog, "arguments": [
                                    ^
        File "/tmp/pip-build-env-xrs7p_m9/overlay/lib/python3.11/site-packages/shtab/__init__.py", line 507, in <listcomp>
          format_optional(opt, parser)
        File "/tmp/pip-build-env-xrs7p_m9/overlay/lib/python3.11/site-packages/shtab/__init__.py", line 488, in format_optional
          (choice_type2fn[opt.choices[0].type] if isinstance(opt.choices[0], Choice) else
                                                             ~~~~~~~~~~~^^^
      TypeError: 'set' object is not subscriptable
casperdcl commented 9 months ago

Duplicate of https://github.com/iterative/shtab/issues/95#issuecomment-1476479655

According to https://docs.python.org/3/library/argparse.html#choices, choices must be a sequence. Sets are not sequences. isinstance(set(), typing.Sequence) == False.