add pass_meta_key decorator
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent 1cb8609 · Browse files · 4 files·+65 added−0 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+65 added−0 removed
- tests:+23 added−0 removed
- other:+42 added−0 removed
Provenance
- Commit
21a68bd- Tree
a009889- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
1cb8609
4 files changed+65 added−0 removed
modified:docs/api.rst+3 added
29 unmodified lines30 .. autofunction:: pass_obj31 32 .. autofunction:: make_pass_decorator33 34+.. autofunction:: click.decorators.pass_meta_key35+36+37 Utilities38 ---------39 40 .. autofunction:: echo169 unmodified linesmodified:src/click/decorators.py+36 added
1 import inspect2+import typing as t3 from functools import update_wrapper4 5 from .core import Argument6 from .core import Command1 unmodified line8 from .core import Option9 from .globals import get_current_context10 from .utils import echo11 12+if t.TYPE_CHECKING:13+ F = t.TypeVar("F", bound=t.Callable[..., t.Any])14 15 def pass_context(f):16 """Marks a callback as wanting to receive the current context17 object as first argument.59 unmodified lines77 78 return decorator79 80 81+def pass_meta_key(82+ key: str, *, doc_description: t.Optional[str] = None83+) -> "t.Callable[[F], F]":84+ """Create a decorator that passes a key from85+ :attr:`click.Context.meta` as the first argument to the decorated86+ function.87+88+ :param key: Key in ``Context.meta`` to pass.89+ :param doc_description: Description of the object being passed,90+ inserted into the decorator's docstring. Defaults to "the 'key'91+ key from Context.meta".92+93+ .. versionadded:: 8.094+ """95+96+ def decorator(f: "F") -> "F":97+ def new_func(*args, **kwargs):98+ ctx = get_current_context()99+ obj = ctx.meta[key]100+ return ctx.invoke(f, obj, *args, **kwargs)101+102+ return update_wrapper(t.cast("F", new_func), f)103+104+ if doc_description is None:105+ doc_description = f"the {key!r} key from :attr:`click.Context.meta`"106+107+ decorator.__doc__ = (108+ f"Decorator that passes {doc_description} as the first argument"109+ " to the decorated function."110+ )111+ return decorator112+113+114 def _make_command(f, name, attrs, cls):115 if isinstance(f, Command):116 raise TypeError("Attempted to convert a callback into a command twice.")117 try:294 unmodified linesmodified:tests/test_context.py+23 added
2 unmodified lines3 import pytest4 5 import click6 from click.core import ParameterSource7+from click.decorators import pass_meta_key8 9 10 def test_ensure_context_objects(runner):11 class Foo:139 unmodified lines151 152 runner.invoke(cli, [], catch_exceptions=False)153 154 155+def test_make_pass_meta_decorator(runner):156+ @click.group()157+ @click.pass_context158+ def cli(ctx):159+ ctx.meta["value"] = "good"160+161+ @cli.command()162+ @pass_meta_key("value")163+ def show(value):164+ return value165+166+ result = runner.invoke(cli, ["show"], standalone_mode=False)167+ assert result.return_value == "good"168+169+170+def test_make_pass_meta_decorator_doc():171+ pass_value = pass_meta_key("value")172+ assert "the 'value' key from :attr:`click.Context.meta`" in pass_value.__doc__173+ pass_value = pass_meta_key("value", doc_description="the test value")174+ assert "passes the test value" in pass_value.__doc__175+176+177 def test_context_pushing():178 rv = []179 180 @click.command()187 unmodified linesmodified:CHANGES.rst+3 added
182 unmodified lines183 - The ``Path`` param type can be passed ``path_type=pathlib.Path`` to184 return a path object instead of a string. :issue:`405`185 - ``TypeError`` is raised when parameter with ``multiple=True`` or186 ``nargs > 1`` has non-iterable default. :issue:`1749`187+- Add a ``pass_meta_key`` decorator for passing a key from188+ ``Context.meta``. This is useful for extensions using ``meta`` to189+ store information. :issue:`1739`190 191 192 Version 7.1.2193 -------------635 unmodified lines