pallets / pallets/click

resolve_command fails if cmd is None

Open
#2,402 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

parsing
Dominant language
Python
Stars
17.7k
Forks
2.3k
Avg merge
1d 30m
Merged PRs (30d)
18

Description

On the documentation, an example is given for how to implement AliasedGroups

We have a command, which is intended to be run like this

axii workspace add

tab completion should then suggest the package names, which works. However, if there is a typo in the command, the command errors out with an ugly stack-trace if implemented according to the documentation.

axii workspaceTYPO add
~/git/axii$ axii workspaceTYPO add Traceback (most recent call last):
  File "/home/tobiasjacob/git/axii/axii", line 7, in <module>
    entry_point()
  File "/home/tobiasjacob/git/axii/.venv/lib/python3.10/site-packages/click/core.py", line 1134, in __call__
    return self.main(*args, **kwargs)
  File "/home/tobiasjacob/git/axii/.venv/lib/python3.10/site-packages/click/core.py", line 1054, in main
    self._main_shell_completion(extra, prog_name, complete_var)
  File "/home/tobiasjacob/git/axii/.venv/lib/python3.10/site-packages/click/core.py", line 1129, in _main_shell_completion
    rv = shell_complete(self, ctx_args, prog_name, complete_var, instruction)
  File "/home/tobiasjacob/git/axii/.venv/lib/python3.10/site-packages/click/shell_completion.py", line 49, in shell_complete
    echo(comp.complete())
  File "/home/tobiasjacob/git/axii/.venv/lib/python3.10/site-packages/click/shell_completion.py", line 291, in complete
    completions = self.get_completions(args, incomplete)
  File "/home/tobiasjacob/git/axii/.venv/lib/python3.10/site-packages/click/shell_completion.py", line 271, in get_completions
    ctx = _resolve_context(self.cli, self.ctx_args, self.prog_name, args)
  File "/home/tobiasjacob/git/axii/.venv/lib/python3.10/site-packages/click/shell_completion.py", line 498, in _resolve_context
    name, cmd, args = command.resolve_command(ctx, args)
  File "/home/tobiasjacob/git/axii/armarx_setup/cli/common.py", line 127, in resolve_command
    return cmd.name # if cmd else None, cmd, args
AttributeError: 'NoneType' object has no attribute 'name'

I suggest one of to changes:

  • Simply removing the resolve_command function solves the issue. This is porably nicer, since the resolve_command function is nowhere else documented.
        def resolve_command(self, ctx, args):
            # always return the full command name
            _, cmd, args = super().resolve_command(ctx, args)
            return cmd.name, cmd, args
    
  • Or changing the example to
        def resolve_command(self, ctx, args):
            # always return the full command name
            _, cmd, args = super().resolve_command(ctx, args)
            return cmd.name if cmd else None, cmd, args
    

solves the issue. Please not, that cmd.name if cmd else None is also implemented in src/click/core.py:1716

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the advanced command aliases example in the documentation and compare it with src/click/core.py:1716. Reproduce shell completion using an invalid command such as workspaceTYPO, then trace resolve_command in the reported example. Done means the typo no longer produces an AttributeError traceback, with the relevant documentation or behavior covered appropriately.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli, documentation
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.