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

Fix arg parameter of autocompletion #1134

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tests/test_others.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def test_completion_untyped_parameters():
},
)
assert "info name is: completion_no_types.py" in result.stderr
assert "args is: []" in result.stderr
assert "args is: ['--name', 'Sebastian', '--name']" in result.stderr
assert "incomplete is: Ca" in result.stderr
assert '"Camila":"The reader of books."' in result.stdout
assert '"Carlos":"The writer of scripts."' in result.stdout
Expand All @@ -202,7 +202,7 @@ def test_completion_untyped_parameters_different_order_correct_names():
},
)
assert "info name is: completion_no_types_order.py" in result.stderr
assert "args is: []" in result.stderr
assert "args is: ['--name', 'Sebastian', '--name']" in result.stderr
assert "incomplete is: Ca" in result.stderr
assert '"Camila":"The reader of books."' in result.stdout
assert '"Carlos":"The writer of scripts."' in result.stdout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_completion():
assert '"Camila":"The reader of books."' in result.stdout
assert '"Carlos":"The writer of scripts."' in result.stdout
assert '"Sebastian":"The type hints guy."' in result.stdout
assert "[]" in result.stderr
assert "--name" in result.stderr


def test_1():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_completion():
assert '"Camila":"The reader of books."' in result.stdout
assert '"Carlos":"The writer of scripts."' in result.stdout
assert '"Sebastian":"The type hints guy."' in result.stdout
assert "[]" in result.stderr
assert "--name" in result.stderr


def test_1():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_completion():
assert '"Camila":"The reader of books."' in result.stdout
assert '"Carlos":"The writer of scripts."' in result.stdout
assert '"Sebastian":"The type hints guy."' not in result.stdout
assert "[]" in result.stderr
assert "['--name', 'Sebastian', '--name']" in result.stderr


def test_1():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_completion():
assert '"Camila":"The reader of books."' in result.stdout
assert '"Carlos":"The writer of scripts."' in result.stdout
assert '"Sebastian":"The type hints guy."' not in result.stdout
assert "[]" in result.stderr
assert "['--name', 'Sebastian', '--name']" in result.stderr


def test_1():
Expand Down
17 changes: 17 additions & 0 deletions typer/_completion_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def get_completion_args(self) -> Tuple[List[str], str]:
except IndexError:
incomplete = ""

obj = self.ctx_args.setdefault("obj", {})
if isinstance(obj, dict):
obj.setdefault("args", args)

return args, incomplete

def format_completion(self, item: click.shell_completion.CompletionItem) -> str:
Expand Down Expand Up @@ -77,6 +81,11 @@ def get_completion_args(self) -> Tuple[List[str], str]:
args = args[:-1]
else:
incomplete = ""

obj = self.ctx_args.setdefault("obj", {})
if isinstance(obj, dict):
obj.setdefault("args", args)

return args, incomplete

def format_completion(self, item: click.shell_completion.CompletionItem) -> str:
Expand Down Expand Up @@ -128,6 +137,11 @@ def get_completion_args(self) -> Tuple[List[str], str]:
args = args[:-1]
else:
incomplete = ""

obj = self.ctx_args.setdefault("obj", {})
if isinstance(obj, dict):
obj.setdefault("args", args)

return args, incomplete

def format_completion(self, item: click.shell_completion.CompletionItem) -> str:
Expand Down Expand Up @@ -177,6 +191,9 @@ def get_completion_args(self) -> Tuple[List[str], str]:
incomplete = os.getenv("_TYPER_COMPLETE_WORD_TO_COMPLETE", "")
cwords = click.parser.split_arg_string(completion_args)
args = cwords[1:-1] if incomplete else cwords[1:]
obj = self.ctx_args.setdefault("obj", {})
if isinstance(obj, dict):
obj.setdefault("args", args)
return args, incomplete

def format_completion(self, item: click.shell_completion.CompletionItem) -> str:
Expand Down
3 changes: 2 additions & 1 deletion typer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,8 @@ def wrapper(ctx: click.Context, args: List[str], incomplete: Optional[str]) -> A
if ctx_name:
use_params[ctx_name] = ctx
if args_name:
use_params[args_name] = args
obj = ctx.obj or {}
use_params[args_name] = obj.get("args", []) if isinstance(obj, dict) else []
if incomplete_name:
use_params[incomplete_name] = incomplete
return callback(**use_params)
Expand Down
Loading