Fix return value and type signature of `shell_completion.add_completion_class` function (#2421)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Alexander Regueiro · committed by GitHub · parent eb1d760 · Browse files · 3 files·+80 added−2 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: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Co-authored-by: Sagi Buchbinder Shadur <saroad2@gmail.com>
Diff stats
3 files+80 added−2 removed
- tests:+72 added−0 removed
- other:+8 added−2 removed
Provenance
- Commit
e002dbc- Tree
9aeeed6- Author
Alexander Regueiro <alexreg@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
eb1d760
3 files changed+80 added−2 removed
modified:src/click/shell_completion.py+7 added-2 removed
387 unmodified lines388 389 return f"{item.type},{item.value}"390 391 392+ShellCompleteType = t.TypeVar("ShellCompleteType", bound=t.Type[ShellComplete])393+394+395 _available_shells: t.Dict[str, t.Type[ShellComplete]] = {396 "bash": BashComplete,397 "fish": FishComplete,398 "zsh": ZshComplete,399 }400 401 402 def add_completion_class(400- cls: t.Type[ShellComplete], name: t.Optional[str] = None401-) -> None:403+ cls: ShellCompleteType, name: t.Optional[str] = None404+) -> ShellCompleteType:405 """Register a :class:`ShellComplete` subclass under the given name.406 The name will be provided by the completion instruction environment407 variable during completion.408 6 unmodified lines415 name = cls.name416 417 _available_shells[name] = cls418 419+ return cls420 421+422 def get_completion_class(shell: str) -> t.Optional[t.Type[ShellComplete]]:423 """Look up a registered :class:`ShellComplete` subclass by the name424 provided by the completion instruction environment variable. If the425 name isn't registered, returns ``None``.160 unmodified linesmodified:tests/test_shell_completion.py+72 added
1 import pytest2 3+import click.shell_completion4 from click.core import Argument5 from click.core import Command6 from click.core import Group7 from click.core import Option8+from click.shell_completion import add_completion_class9 from click.shell_completion import CompletionItem10 from click.shell_completion import ShellComplete11 from click.types import Choice12 from click.types import File330 unmodified lines343 params=[Option(["-a"], type=Choice(["Au", "al", "Bc"], case_sensitive=value))],344 )345 completions = _get_words(cli, ["-a"], "a")346 assert completions == expect347+348+349+@pytest.fixture()350+def _restore_available_shells(tmpdir):351+ prev_available_shells = click.shell_completion._available_shells.copy()352+ click.shell_completion._available_shells.clear()353+ yield354+ click.shell_completion._available_shells.clear()355+ click.shell_completion._available_shells.update(prev_available_shells)356+357+358+@pytest.mark.usefixtures("_restore_available_shells")359+def test_add_completion_class():360+ # At first, "mysh" is not in available shells361+ assert "mysh" not in click.shell_completion._available_shells362+363+ class MyshComplete(ShellComplete):364+ name = "mysh"365+ source_template = "dummy source"366+367+ # "mysh" still not in available shells because it is not registered368+ assert "mysh" not in click.shell_completion._available_shells369+370+ # Adding a completion class should return that class371+ assert add_completion_class(MyshComplete) is MyshComplete372+373+ # Now, "mysh" is finally in available shells374+ assert "mysh" in click.shell_completion._available_shells375+ assert click.shell_completion._available_shells["mysh"] is MyshComplete376+377+378+@pytest.mark.usefixtures("_restore_available_shells")379+def test_add_completion_class_with_name():380+ # At first, "mysh" is not in available shells381+ assert "mysh" not in click.shell_completion._available_shells382+ assert "not_mysh" not in click.shell_completion._available_shells383+384+ class MyshComplete(ShellComplete):385+ name = "not_mysh"386+ source_template = "dummy source"387+388+ # "mysh" and "not_mysh" are still not in available shells because389+ # it is not registered yet390+ assert "mysh" not in click.shell_completion._available_shells391+ assert "not_mysh" not in click.shell_completion._available_shells392+393+ # Adding a completion class should return that class.394+ # Because we are using the "name" parameter, the name isn't taken395+ # from the class.396+ assert add_completion_class(MyshComplete, name="mysh") is MyshComplete397+398+ # Now, "mysh" is finally in available shells399+ assert "mysh" in click.shell_completion._available_shells400+ assert "not_mysh" not in click.shell_completion._available_shells401+ assert click.shell_completion._available_shells["mysh"] is MyshComplete402+403+404+@pytest.mark.usefixtures("_restore_available_shells")405+def test_add_completion_class_decorator():406+ # At first, "mysh" is not in available shells407+ assert "mysh" not in click.shell_completion._available_shells408+409+ @add_completion_class410+ class MyshComplete(ShellComplete):411+ name = "mysh"412+ source_template = "dummy source"413+414+ # Using `add_completion_class` as a decorator adds the new shell immediately415+ assert "mysh" in click.shell_completion._available_shells416+ assert click.shell_completion._available_shells["mysh"] is MyshCompletemodified:CHANGES.rst+1 added
5 unmodified lines6 Unreleased7 8 - Improve type hinting for decorators and give all generic types parameters.9 :issue:`2398`10+- Fix return value and type signature of `shell_completion.add_completion_class` function. :pr:`2421`11 12 13 Version 8.1.314 -------------1040 unmodified lines