Skip to content
Myr

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

4 files+65 added−0 removed

modified:docs/api.rst+3 added
29 unmodified lines
30.. autofunction:: pass_obj3132.. autofunction:: make_pass_decorator3334.. autofunction:: click.decorators.pass_meta_key353637Utilities38---------3940.. autofunction:: echo
169 unmodified lines
modified:src/click/decorators.py+36 added
1import inspect2import typing as t3from functools import update_wrapper45from .core import Argument6from .core import Command
1 unmodified line
8from .core import Option9from .globals import get_current_context10from .utils import echo1112if t.TYPE_CHECKING:13 F = t.TypeVar("F", bound=t.Callable[..., t.Any])1415def pass_context(f):16 """Marks a callback as wanting to receive the current context17 object as first argument.
59 unmodified lines
7778 return decorator798081def 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.8788 :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".9293 .. versionadded:: 8.094 """9596 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)101102 return update_wrapper(t.cast("F", new_func), f)103104 if doc_description is None:105 doc_description = f"the {key!r} key from :attr:`click.Context.meta`"106107 decorator.__doc__ = (108 f"Decorator that passes {doc_description} as the first argument"109 " to the decorated function."110 )111 return decorator112113114def _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 lines
modified:tests/test_context.py+23 added
2 unmodified lines
3import pytest45import click6from click.core import ParameterSource7from click.decorators import pass_meta_key8910def test_ensure_context_objects(runner):11 class Foo:
139 unmodified lines
151152 runner.invoke(cli, [], catch_exceptions=False)153154155def test_make_pass_meta_decorator(runner):156 @click.group()157 @click.pass_context158 def cli(ctx):159 ctx.meta["value"] = "good"160161 @cli.command()162 @pass_meta_key("value")163 def show(value):164 return value165166 result = runner.invoke(cli, ["show"], standalone_mode=False)167 assert result.return_value == "good"168169170def 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__175176177def test_context_pushing():178 rv = []179180 @click.command()
187 unmodified lines
modified:CHANGES.rst+3 added
182 unmodified lines
183- 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`190191192Version 7.1.2193-------------
635 unmodified lines