Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit736f9ec

Browse files
authored
Add Aliasing Decorators section to libraries.rst (#1243)
1 parentbe7ba97 commit736f9ec

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

‎docs/guides/libraries.rst

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,8 @@ specified only by name, use the keyword-only separator (``*``).
412412
defcreate_user(age:int,*,dob: Optional[date]=None):
413413
...
414414
415+
.. _annotating-decorators:
416+
415417
Annotating Decorators
416418
---------------------
417419

@@ -448,6 +450,66 @@ original signature, thus blinding type checkers and other tools that
448450
provide signature assistance. As such, library authors are discouraged
449451
from creating decorators that mutate function signatures in this manner.
450452

453+
.. _aliasing-decorators:
454+
455+
Aliasing Decorators
456+
-------------------
457+
458+
When writing a library with a couple of decorator factories
459+
(i.e. functions returning decorators, like ``complex_decorator`` from the
460+
:ref:`annotating-decorators` section) it may be tempting to create a shortcut
461+
for a decorator.
462+
463+
Different type checkers handle:data:`TypeAlias <typing.TypeAlias>` involving
464+
:class:`Callable <collections.abc.Callable>` in a
465+
different manner, so the most portable and easy way to create a shortcut
466+
is to define a callable:class:`Protocol <typing.Protocol>` as described in the
467+
:ref:`callback-protocols` section of the Typing Specification.
468+
469+
There is already a:class:`Protocol <typing.Protocol>` called
470+
``IdentityFunction`` defined in
471+
`_typeshed<https://github.com/python/typeshed/blob/main/stdlib/_typeshed/README.md>`_:
472+
473+
..code::python
474+
475+
from typingimportTYPE_CHECKING
476+
477+
ifTYPE_CHECKING:
478+
from _typeshedimport IdentityFunction
479+
480+
defdecorator_factory(*,mode:str) ->"IdentityFunction":
481+
"""
482+
Decorator factory is invoked with arguments like this:
483+
@decorator_factory(mode="easy")
484+
def my_function(): ...
485+
"""
486+
...
487+
488+
For non-trivial decorators with custom logic, it is still possible
489+
to define a custom protocol using:class:`ParamSpec <typing.ParamSpec>`
490+
and:data:`Concatenate <typing.Concatenate>` mechanisms:
491+
492+
..code::python
493+
494+
classClient:...
495+
496+
P= ParamSpec("P")
497+
R= TypeVar("R")
498+
499+
classPClientInjector(Protocol):
500+
def__call__(self,_: Callable[Concatenate[Client, P], R],/) -> Callable[P, R]:
501+
...
502+
503+
definject_client(service:str) -> PClientInjector:
504+
"""
505+
Decorator factory is invoked with arguments like this:
506+
@inject_client("testing")
507+
def my_function(client: Client, value: int): ...
508+
509+
my_function then takes only value
510+
"""
511+
512+
451513
Generic Classes and Functions
452514
-----------------------------
453515

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp