Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Aliasing Decorators section to libraries.rst #1243

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions docs/source/libraries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,60 @@ original signature, thus blinding type checkers and other tools that
provide signature assistance. As such, library authors are discouraged
from creating decorators that mutate function signatures in this manner.

Aliasing Decorators
-------------------

When writing a library with a couple of decorator factories
(i.e. functions returning decorators, like ``complex_decorator`` from
:ref:`annotating-decorators` section) it may be tempting to create a shortcut
for a decorator.

Different type checkers handle ``TypeAlias`` involving ``Callable`` in a
different manner, so the most portable and easy way to create a shortcut
is to define a callable ``Protocol`` as described in `PEP
544 <https://peps.python.org/pep-0544/#callback-protocols>`_.

There is already a ``Protcol`` called ``IndentityFunction`` defined in ``_typeshed``:
Drino marked this conversation as resolved.
Show resolved Hide resolved

.. code:: python

if TYPE_CHECKING:
from _typeshed import IdentityFunction

def decorator_factory(*, mode: str) -> "IdentityFunction":
"""
Decorator factory is invoked with arguments like this:
@decorator_factory(mode="easy")
def my_function(): ...
"""
...

For non-trivial decorators with custom logic, it is still possible
to define a custom protocol using ``ParamSpec`` and ``Concatenate``
mechanisms described in `PEP 612
<https://www.python.org/dev/peps/pep-0612/>`__:

.. code:: python

class Client: ...

P = ParamSpec("P")
R = TypeVar("R")

class PClientInjector(Protocol):
def __call__(self, _: Callable[Concatenate[Client, P], R], /) -> Callable[P, R]:
...

def inject_client(service: str) -> PClientInjector:
"""
Decorator factory is invoked with arguments like this:
@inject_client("testing")
def my_function(client: Client, value: int): ...

my_function then takes only value
"""


Generic Classes and Functions
-----------------------------

Expand Down