Skip to content
Myr

🐛 Allow colon in zsh autocomplete values and descriptions (#988)

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

authored by Matt Crane · committed by GitHub · parent 438ae44 · Browse files · 3 files+245 added−0 removed
This commit

State

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

Trailers

  • Co-authored-by: Matt Crane <matt.crane@airbnb.com>
  • Co-authored-by: svlandeg <svlandeg@github.com>

Diff stats

3 files+245 added−0 removed

  • tests:+244 added−0 removed
  • other:+1 added−0 removed

3 files+245 added−0 removed

added:tests/test_completion/colon_example.py+25 added
1import typer23image_desc = [4 ("alpine:latest", "latest alpine image"),5 ("alpine:hello", "fake image: for testing"),6 ("nvidia/cuda:10.0-devel-ubuntu18.04", ""),7]8910def _complete(incomplete: str) -> str:11 for image, desc in image_desc:12 if image.startswith(incomplete):13 yield image, desc141516app = typer.Typer()171819@app.command()20def image(name: str = typer.Option(autocompletion=_complete)):21 typer.echo(name)222324if __name__ == "__main__":25 app()
added:tests/test_completion/test_completion_option_colon.py+219 added
1import os2import subprocess3import sys45from . import colon_example as mod678def test_script():9 result = subprocess.run(10 [sys.executable, "-m", "coverage", "run", mod.__file__, "--name", "DeadPool"],11 capture_output=True,12 encoding="utf-8",13 )14 assert result.returncode == 015 assert "DeadPool" in result.stdout161718def test_completion_colon_bash_all():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 "_COLON_EXAMPLE.PY_COMPLETE": "complete_bash",26 "COMP_WORDS": "colon_example.py --name ",27 "COMP_CWORD": "2",28 },29 )30 assert "alpine:hello" in result.stdout31 assert "alpine:latest" in result.stdout32 assert "nvidia/cuda:10.0-devel-ubuntu18.04" in result.stdout333435def test_completion_colon_bash_partial():36 result = subprocess.run(37 [sys.executable, "-m", "coverage", "run", mod.__file__, " "],38 capture_output=True,39 encoding="utf-8",40 env={41 **os.environ,42 "_COLON_EXAMPLE.PY_COMPLETE": "complete_bash",43 "COMP_WORDS": "colon_example.py --name alpine ",44 "COMP_CWORD": "2",45 },46 )47 assert "alpine:hello" in result.stdout48 assert "alpine:latest" in result.stdout49 assert "nvidia/cuda:10.0-devel-ubuntu18.04" not in result.stdout505152def test_completion_colon_bash_single():53 result = subprocess.run(54 [sys.executable, "-m", "coverage", "run", mod.__file__, " "],55 capture_output=True,56 encoding="utf-8",57 env={58 **os.environ,59 "_COLON_EXAMPLE.PY_COMPLETE": "complete_bash",60 "COMP_WORDS": "colon_example.py --name alpine:hell ",61 "COMP_CWORD": "2",62 },63 )64 assert "alpine:hello" in result.stdout65 assert "alpine:latest" not in result.stdout66 assert "nvidia/cuda:10.0-devel-ubuntu18.04" not in result.stdout676869def test_completion_colon_zsh_all():70 result = subprocess.run(71 [sys.executable, "-m", "coverage", "run", mod.__file__, " "],72 capture_output=True,73 encoding="utf-8",74 env={75 **os.environ,76 "_COLON_EXAMPLE.PY_COMPLETE": "complete_zsh",77 "_TYPER_COMPLETE_ARGS": "colon_example.py --name ",78 },79 )80 assert "alpine\\\\:hello" in result.stdout81 assert "alpine\\\\:latest" in result.stdout82 assert "nvidia/cuda\\\\:10.0-devel-ubuntu18.04" in result.stdout838485def test_completion_colon_zsh_partial():86 result = subprocess.run(87 [sys.executable, "-m", "coverage", "run", mod.__file__, " "],88 capture_output=True,89 encoding="utf-8",90 env={91 **os.environ,92 "_COLON_EXAMPLE.PY_COMPLETE": "complete_zsh",93 "_TYPER_COMPLETE_ARGS": "colon_example.py --name alpine",94 },95 )96 assert "alpine\\\\:hello" in result.stdout97 assert "alpine\\\\:latest" in result.stdout98 assert "nvidia/cuda\\\\:10.0-devel-ubuntu18.04" not in result.stdout99100101def test_completion_colon_zsh_single():102 result = subprocess.run(103 [sys.executable, "-m", "coverage", "run", mod.__file__, " "],104 capture_output=True,105 encoding="utf-8",106 env={107 **os.environ,108 "_COLON_EXAMPLE.PY_COMPLETE": "complete_zsh",109 "_TYPER_COMPLETE_ARGS": "colon_example.py --name alpine:hell",110 },111 )112 assert "alpine\\\\:hello" in result.stdout113 assert "alpine\\\\:latest" not in result.stdout114 assert "nvidia/cuda\\\\:10.0-devel-ubuntu18.04" not in result.stdout115116117def test_completion_colon_powershell_all():118 result = subprocess.run(119 [sys.executable, "-m", "coverage", "run", mod.__file__, " "],120 capture_output=True,121 encoding="utf-8",122 env={123 **os.environ,124 "_COLON_EXAMPLE.PY_COMPLETE": "complete_powershell",125 "_TYPER_COMPLETE_ARGS": "colon_example.py --name ",126 "_TYPER_COMPLETE_WORD_TO_COMPLETE": "",127 },128 )129 assert "alpine:hello" in result.stdout130 assert "alpine:latest" in result.stdout131 assert "nvidia/cuda:10.0-devel-ubuntu18.04" in result.stdout132133134def test_completion_colon_powershell_partial():135 result = subprocess.run(136 [sys.executable, "-m", "coverage", "run", mod.__file__, " "],137 capture_output=True,138 encoding="utf-8",139 env={140 **os.environ,141 "_COLON_EXAMPLE.PY_COMPLETE": "complete_powershell",142 "_TYPER_COMPLETE_ARGS": "colon_example.py --name alpine",143 "_TYPER_COMPLETE_WORD_TO_COMPLETE": "alpine",144 },145 )146 assert "alpine:hello" in result.stdout147 assert "alpine:latest" in result.stdout148 assert "nvidia/cuda:10.0-devel-ubuntu18.04" not in result.stdout149150151def test_completion_colon_powershell_single():152 result = subprocess.run(153 [sys.executable, "-m", "coverage", "run", mod.__file__, " "],154 capture_output=True,155 encoding="utf-8",156 env={157 **os.environ,158 "_COLON_EXAMPLE.PY_COMPLETE": "complete_powershell",159 "_TYPER_COMPLETE_ARGS": "colon_example.py --name alpine:hell",160 "_TYPER_COMPLETE_WORD_TO_COMPLETE": "alpine:hell",161 },162 )163 assert "alpine:hello" in result.stdout164 assert "alpine:latest" not in result.stdout165 assert "nvidia/cuda:10.0-devel-ubuntu18.04" not in result.stdout166167168def test_completion_colon_pwsh_all():169 result = subprocess.run(170 [sys.executable, "-m", "coverage", "run", mod.__file__, " "],171 capture_output=True,172 encoding="utf-8",173 env={174 **os.environ,175 "_COLON_EXAMPLE.PY_COMPLETE": "complete_pwsh",176 "_TYPER_COMPLETE_ARGS": "colon_example.py --name",177 },178 )179180 assert "alpine:hello" in result.stdout181 assert "alpine:latest" in result.stdout182 assert "nvidia/cuda:10.0-devel-ubuntu18.04" in result.stdout183184185def test_completion_colon_pwsh_partial():186 result = subprocess.run(187 [sys.executable, "-m", "coverage", "run", mod.__file__, " "],188 capture_output=True,189 encoding="utf-8",190 env={191 **os.environ,192 "_COLON_EXAMPLE.PY_COMPLETE": "complete_pwsh",193 "_TYPER_COMPLETE_ARGS": "colon_example.py --name alpine",194 "_TYPER_COMPLETE_WORD_TO_COMPLETE": "alpine",195 },196 )197 assert "alpine:hello" in result.stdout198 assert "alpine:latest" in result.stdout199 assert "nvidia/cuda:10.0-devel-ubuntu18.04" not in result.stdout200201202def test_completion_colon_pwsh_single():203 result = subprocess.run(204 [sys.executable, "-m", "coverage", "run", mod.__file__, " "],205 capture_output=True,206 encoding="utf-8",207 env={208 **os.environ,209 "_COLON_EXAMPLE.PY_COMPLETE": "complete_pwsh",210 "_TYPER_COMPLETE_ARGS": "colon_example.py --name alpine:hell",211 "_TYPER_COMPLETE_WORD_TO_COMPLETE": "alpine:hell",212 },213 )214 assert "alpine:hello" in result.stdout215 assert "alpine:latest" not in result.stdout216 assert "nvidia/cuda:10.0-devel-ubuntu18.04" not in result.stdout217218219# TODO: tests for complete_fish
modified:typer/_completion_classes.py+1 added
84 unmodified lines
85 s.replace('"', '""')86 .replace("'", "''")87 .replace("$", "\\$")88 .replace("`", "\\`")89 .replace(":", r"\\:")90 )9192 # TODO: Explore replicating the new behavior from Click, pay attention to93 # the difference with and without escape
100 unmodified lines