Skip to content
Myr

🐛 Ensure that autocompletion works for `Path` arguments/options (#1138)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sofie Van Landeghem · committed by GitHub · parent 3c9f2c0 · Browse files · 4 files+57 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+57 added−1 removed

  • tests:+44 added−0 removed
  • other:+13 added−1 removed

4 files+57 added−1 removed

added:tests/test_completion/path_example.py+14 added
1from pathlib import Path23import typer45app = typer.Typer()678@app.command()9def f(p: Path):10 print(p)111213if __name__ == "__main__":14 app()
added:tests/test_completion/test_completion_path.py+30 added
1import os2import subprocess3import sys45from . import path_example as mod678def test_script():9 result = subprocess.run(10 [sys.executable, "-m", "coverage", "run", mod.__file__, "path/to/deadpool"],11 capture_output=True,12 encoding="utf-8",13 )14 assert result.returncode == 015 assert "deadpool" in result.stdout161718def test_completion_path_bash():19 result = subprocess.run(20 [sys.executable, "-m", "coverage", "run", mod.__file__, " "],21 capture_output=True,22 encoding="utf-8",23 env={24 **os.environ,25 "_PATH_EXAMPLE.PY_COMPLETE": "complete_bash",26 "COMP_WORDS": "path_example.py ",27 "COMP_CWORD": "2",28 },29 )30 assert result.returncode == 0
modified:typer/main.py+2 added-1 removed
42 unmodified lines
43 ParameterInfo,44 ParamMeta,45 Required,46 TyperInfo,47 TyperPath,48)49from .utils import get_params_from_function5051try:
691 unmodified lines
743 or parameter_info.allow_dash744 or parameter_info.path_type745 or parameter_info.resolve_path746 ):746 return click.Path(747 return TyperPath(748 exists=parameter_info.exists,749 file_okay=parameter_info.file_okay,750 dir_okay=parameter_info.dir_okay,751 writable=parameter_info.writable,
388 unmodified lines
modified:typer/models.py+11 added
529 unmodified lines
530 ) -> None:531 self.pretty_exceptions_enable = pretty_exceptions_enable532 self.pretty_exceptions_show_locals = pretty_exceptions_show_locals533 self.pretty_exceptions_short = pretty_exceptions_short534535536class TyperPath(click.Path):537 # Overwrite Click's behaviour to be compatible with Typer's autocompletion system538 def shell_complete(539 self, ctx: click.Context, param: click.Parameter, incomplete: str540 ) -> List[click.shell_completion.CompletionItem]:541 """Return an empty list so that the autocompletion functionality542 will work properly from the commandline.543 """544 return []