Skip to content

Commit

Permalink
revert bug fix for args parameter in completer functions, this will b…
Browse files Browse the repository at this point in the history
…e included in a separate PR. Also update tutorial 9 to show all supported parameters may be passed
  • Loading branch information
bckohan committed Jan 24, 2025
1 parent 4c356f5 commit c4ef5ab
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 29 deletions.
2 changes: 1 addition & 1 deletion docs/tutorial/options-autocompletion.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ But it's probably useful only in very advanced use cases.

## Getting the Context and the raw *CLI parameters*

Of course, you can declare everything if you need it, the context, the raw *CLI parameters*, and the incomplete `str`:
Of course, you can declare everything if you need it, the context, the raw *CLI parameters*, the Parameter and the incomplete `str`:

{* docs_src/options_autocompletion/tutorial009_an.py hl[16] *}

Expand Down
5 changes: 4 additions & 1 deletion docs_src/options_autocompletion/tutorial009.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
err_console = Console(stderr=True)


def complete_name(ctx: typer.Context, param: Parameter, incomplete: str):
def complete_name(
ctx: typer.Context, args: List[str], param: Parameter, incomplete: str
):
err_console.print(f"{args}")
names = ctx.params.get(param.name) or []
for name, help_text in valid_completion_items:
if name.startswith(incomplete) and name not in names:
Expand Down
5 changes: 4 additions & 1 deletion docs_src/options_autocompletion/tutorial009_an.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
err_console = Console(stderr=True)


def complete_name(ctx: typer.Context, param: Parameter, incomplete: str):
def complete_name(
ctx: typer.Context, args: List[str], param: Parameter, incomplete: str
):
err_console.print(f"{args}")
names = ctx.params.get(param.name) or []
for name, help_text in valid_completion_items:
if name.startswith(incomplete) and name not in names:
Expand Down
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: ['--name', 'Sebastian', '--name']" in result.stderr
assert "args is: []" in result.stderr
assert "param is: name" in result.stderr
assert "incomplete is: Ca" in result.stderr
assert '"Camila":"The reader of books."' in result.stdout
Expand All @@ -203,7 +203,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: ['--name', 'Sebastian', '--name']" in result.stderr
assert "args is: []" in result.stderr
assert "param is: name" in result.stderr
assert "incomplete is: Ca" in result.stderr
assert '"Camila":"The reader of books."' 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 "--name" in result.stderr
assert "[]" 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 "--name" in result.stderr
assert "[]" in result.stderr


def test_1():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +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


def test_1():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +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


def test_1():
Expand Down
18 changes: 0 additions & 18 deletions typer/_completion_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ 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 @@ -80,11 +77,6 @@ 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 @@ -136,11 +128,6 @@ 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 @@ -190,11 +177,6 @@ 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
2 changes: 1 addition & 1 deletion typer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def compat_autocompletion(

out = []

for c in autocompletion(ctx, param, incomplete):
for c in autocompletion(ctx, [], param, incomplete):
if isinstance(c, tuple):
use_completion = CompletionItem(c[0], help=c[1])
elif isinstance(c, CompletionItem):
Expand Down
8 changes: 5 additions & 3 deletions typer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1062,14 +1062,16 @@ def get_param_completion(
)

def wrapper(
ctx: click.Context, param: click.core.Parameter, incomplete: Optional[str]
ctx: click.Context,
args: List[str],
param: click.core.Parameter,
incomplete: Optional[str],
) -> Any:
use_params: Dict[str, Any] = {}
if ctx_name:
use_params[ctx_name] = ctx
if args_name:
obj = ctx.obj or {}
use_params[args_name] = obj.get("args", []) if isinstance(obj, dict) else []
use_params[args_name] = args
if param_name:
use_params[param_name] = param
if incomplete_name:
Expand Down

0 comments on commit c4ef5ab

Please sign in to comment.