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

resolve_command fails if cmd is None #2402

Open
TobiasJacob opened this issue Nov 9, 2022 · 0 comments
Open

resolve_command fails if cmd is None #2402

TobiasJacob opened this issue Nov 9, 2022 · 0 comments
Labels

Comments

@TobiasJacob
Copy link

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

@davidism davidism added the docs label Jul 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants