With argument autocompletion, context is not passed properly
See original GitHub issueI used the current master revision:
$ pip3 install git+https://github.com/pallets/click.git@55682f6f5348f5220a557f89c3a796321a52aebf
and an executable file testclick
:
#!/usr/bin/env python3
import click
def _complete(ctx, args, incomplete):
return ctx.obj['completions']
@click.group()
@click.pass_context
def entrypoint(ctx):
pass
@entrypoint.command()
@click.argument('arg', type=click.STRING, autocompletion=_complete)
@click.pass_context
def subcommand(ctx, arg):
print('arg={}'.format(arg))
entrypoint(obj={'completions': ['abc', 'def', 'ghi', ]})
and enabled bash completion with
eval "$(_TESTCLICK_COMPLETE=source ./testclick)"
Now I am getting an error when trying to use autocompletion:
$ ./testclick subcommand <TAB>Traceback (most recent call last):
File "./testclick", line 23, in <module>
entrypoint(obj={'completions': ['abc', 'def', 'ghi', ]})
File "/home/user/testclick/venv/lib/python3.6/site-packages/click/core.py", line 731, in __call__
return self.main(*args, **kwargs)
File "/home/user/testclick/venv/lib/python3.6/site-packages/click/core.py", line 701, in main
_bashcomplete(self, prog_name, complete_var)
File "/home/user/testclick/venv/lib/python3.6/site-packages/click/core.py", line 46, in _bashcomplete
if bashcomplete(cmd, prog_name, complete_var, complete_instr):
File "/home/user/testclick/venv/lib/python3.6/site-packages/click/_bashcomplete.py", line 216, in bashcomplete
return do_complete(cli, prog_name)
File "/home/user/testclick/venv/lib/python3.6/site-packages/click/_bashcomplete.py", line 205, in do_complete
for item in get_choices(cli, prog_name, args, incomplete):
File "/home/user/testclick/venv/lib/python3.6/site-packages/click/_bashcomplete.py", line 186, in get_choices
ctx, all_args, incomplete, param))
File "/home/user/testclick/venv/lib/python3.6/site-packages/click/_bashcomplete.py", line 124, in get_user_autocompletions
incomplete=incomplete)
File "./testclick", line 7, in _complete
return ctx.obj['completions']
TypeError: 'NoneType' object is not subscriptable
Expected behavior was to get a list of completions abc def ghi
.
I am using Python 3.6.4
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:8
Top Results From Across the Web
Autocomplete Not Working - Google App Script - Stack Overflow
When working with them you get autocomplete hints, unless you pass such object instance to function as an argument. The context is lost...
Read more >Changes — Click Documentation (8.1.x)
Fixed default parameters not being handled properly by the context invoke method. This is a backwards incompatible change if the function was ...
Read more >Autocompletion in GraphQL resolvers with JavaScript - Prisma
Problem. When using GraphQL with TypeScript, you always get autocompletion for the Prisma Client instance in your GraphQL resolvers because then the context...
Read more >How do I get bash completion for command aliases?
This answer also has another problem: it cannot autocomplete with the context of arguments provided. E.g. it cannot make an alias for pass...
Read more >Argument completion made easy with Visual Studio IntelliCode
why not support autocomplete function “()”? Mark Wilson-Thomas Microsoft employee March 13, 2019 1: ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I don’t understand what LuckyJosh is trying to say.
The custom context with
obj
attached is normally created in BaseCommand.main(). The problem is that in autocomplete mode, _bashcomplete() gets called before the context-creation code executes, and creates its own context (withoutobj
) instead.It looks to me like we want BaseCommand.main() to create its context in all circumstances, and pass it to _bashcomplete(). Would there be any drawbacks to this?
Possibly related: #930
+1 on this issue. I, too, am trying to get autocomplete working with completions that come from network calls.
I’m facing this issue too.
For now, my workaround is to factor out the
ctx.obj
creation code and call it from bothcli()
and the corresponding autocomplete methods.