diff --git a/docs/source/kinds_of_types.rst b/docs/source/kinds_of_types.rst index b575a6eac4c5..c3180850f119 100644 --- a/docs/source/kinds_of_types.rst +++ b/docs/source/kinds_of_types.rst @@ -200,6 +200,28 @@ using bidirectional type inference: If you want to give the argument or return value types explicitly, use an ordinary, perhaps nested function definition. +Callables can also be used against type objects, matching their +``__init__`` or ``__new__`` signature: + +.. code-block:: python + + from typing import Callable + + class C: + def __init__(self, app: str) -> None: + pass + + CallableType = Callable[[str], C] + + def class_or_callable(arg: CallableType) -> None: + inst = arg("my_app") + reveal_type(inst) # Revealed type is "C" + +This is useful if you want ``arg`` to be either a ``Callable`` returning an +instance of ``C`` or the type of ``C`` itself. This also works with +:ref:`callback protocols `. + + .. _union-types: Union types diff --git a/docs/source/protocols.rst b/docs/source/protocols.rst index cb51809a66d5..95b870265f73 100644 --- a/docs/source/protocols.rst +++ b/docs/source/protocols.rst @@ -319,7 +319,7 @@ member: batch_proc([], bad_cb) # Error! Argument 2 has incompatible type because of # different name and kind in the callback -Callback protocols and :py:data:`~typing.Callable` types can be used interchangeably. +Callback protocols and :py:data:`~typing.Callable` types can be used mostly interchangeably. Argument names in :py:meth:`__call__ ` methods must be identical, unless a double underscore prefix is used. For example: