click
click copied to clipboard
resolve_command fails if cmd is None
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_commandfunction solves the issue. This is porably nicer, since theresolve_commandfunction 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