Skip to content
Myr

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

3 files+80 added−2 removed

modified:src/click/shell_completion.py+7 added-2 removed
387 unmodified lines
388389 return f"{item.type},{item.value}"390391392ShellCompleteType = t.TypeVar("ShellCompleteType", bound=t.Type[ShellComplete])393394395_available_shells: t.Dict[str, t.Type[ShellComplete]] = {396 "bash": BashComplete,397 "fish": FishComplete,398 "zsh": ZshComplete,399}400401402def 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 lines
415 name = cls.name416417 _available_shells[name] = cls418419 return cls420421422def 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 lines
modified:tests/test_shell_completion.py+72 added
1import pytest23import click.shell_completion4from click.core import Argument5from click.core import Command6from click.core import Group7from click.core import Option8from click.shell_completion import add_completion_class9from click.shell_completion import CompletionItem10from click.shell_completion import ShellComplete11from click.types import Choice12from click.types import File
330 unmodified lines
343 params=[Option(["-a"], type=Choice(["Au", "al", "Bc"], case_sensitive=value))],344 )345 completions = _get_words(cli, ["-a"], "a")346 assert completions == expect347348349@pytest.fixture()350def _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)356357358@pytest.mark.usefixtures("_restore_available_shells")359def test_add_completion_class():360 # At first, "mysh" is not in available shells361 assert "mysh" not in click.shell_completion._available_shells362363 class MyshComplete(ShellComplete):364 name = "mysh"365 source_template = "dummy source"366367 # "mysh" still not in available shells because it is not registered368 assert "mysh" not in click.shell_completion._available_shells369370 # Adding a completion class should return that class371 assert add_completion_class(MyshComplete) is MyshComplete372373 # Now, "mysh" is finally in available shells374 assert "mysh" in click.shell_completion._available_shells375 assert click.shell_completion._available_shells["mysh"] is MyshComplete376377378@pytest.mark.usefixtures("_restore_available_shells")379def 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_shells383384 class MyshComplete(ShellComplete):385 name = "not_mysh"386 source_template = "dummy source"387388 # "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_shells392393 # 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 MyshComplete397398 # 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 MyshComplete402403404@pytest.mark.usefixtures("_restore_available_shells")405def test_add_completion_class_decorator():406 # At first, "mysh" is not in available shells407 assert "mysh" not in click.shell_completion._available_shells408409 @add_completion_class410 class MyshComplete(ShellComplete):411 name = "mysh"412 source_template = "dummy source"413414 # 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 MyshComplete
modified:CHANGES.rst+1 added
5 unmodified lines
6Unreleased78- 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`111213Version 8.1.314-------------
1040 unmodified lines