typing for pass decorators
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 21a68bd · Browse files · 2 files·+14 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+14 added−7 removed
- other:+14 added−7 removed
Provenance
- Commit
d2b315a- Tree
4417c81- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
21a68bd
2 files changed+14 added−7 removed
modified:docs/conf.py+1 added
22 unmodified lines23 "pallets_sphinx_themes",24 "sphinx_issues",25 "sphinx_tabs.tabs",26 ]27+autodoc_typehints = "description"28 intersphinx_mapping = {"python": ("https://docs.python.org/3/", None)}29 issues_github_path = "pallets/click"30 31 # HTML -----------------------------------------------------------------28 unmodified linesmodified:src/click/decorators.py+13 added-7 removed
10 unmodified lines11 12 if t.TYPE_CHECKING:13 F = t.TypeVar("F", bound=t.Callable[..., t.Any])14 15+15-def pass_context(f):16+def pass_context(f: "F") -> "F":17 """Marks a callback as wanting to receive the current context18 object as first argument.19 """20 21 def new_func(*args, **kwargs):22 return f(get_current_context(), *args, **kwargs)23 23- return update_wrapper(new_func, f)24+ return update_wrapper(t.cast("F", new_func), f)25 26 26-def pass_obj(f):27+def pass_obj(f: "F") -> "F":28 """Similar to :func:`pass_context`, but only pass the object on the29 context onwards (:attr:`Context.obj`). This is useful if that object30 represents the state of a nested system.31 """32 33 def new_func(*args, **kwargs):34 return f(get_current_context().obj, *args, **kwargs)35 35- return update_wrapper(new_func, f)36+ return update_wrapper(t.cast("F", new_func), f)37 38 38-def make_pass_decorator(object_type, ensure=False):39+def make_pass_decorator(40+ object_type: t.Type, ensure: bool = False41+) -> "t.Callable[[F], F]":42 """Given an object type this creates a decorator that will work43 similar to :func:`pass_obj` but instead of passing the object of the44 current context, it will find the innermost context of type45 :func:`object_type`.14 unmodified lines60 :param ensure: if set to `True`, a new object will be created and61 remembered on the context if it's not there yet.62 """63 61- def decorator(f):64+ def decorator(f: "F") -> "F":65 def new_func(*args, **kwargs):66 ctx = get_current_context()67+68 if ensure:69 obj = ctx.ensure_object(object_type)70 else:71 obj = ctx.find_object(object_type)72+73 if obj is None:74 raise RuntimeError(75 "Managed to invoke callback without a context"76 f" object of type {object_type.__name__!r}"77 " existing."78 )79+80 return ctx.invoke(f, obj, *args, **kwargs)81 76- return update_wrapper(new_func, f)82+ return update_wrapper(t.cast("F", new_func), f)83 84 return decorator85 86 331 unmodified lines