@@ -591,7 +591,7 @@ Let us illustrate this by few simple examples:
591591 Covariance should feel relatively intuitive, but contravariance and invariance
592592 can be harder to reason about.
593593
594- * :py:data : `~typing .Callable ` is an example of type that behaves contravariant
594+ * :py:class : `~collections.abc .Callable ` is an example of type that behaves contravariant
595595 in types of arguments. That is, ``Callable[[Shape], int] `` is a subtype of
596596 ``Callable[[Triangle], int] ``, despite ``Shape `` being a supertype of
597597 ``Triangle ``. To understand this, consider:
@@ -833,7 +833,8 @@ Here's how one could annotate the decorator (Python 3.12 syntax):
833833
834834.. code-block :: python
835835
836- from typing import Any, Callable, cast
836+ from collections.abc import Callable
837+ from typing import Any, cast
837838
838839 # A decorator that preserves the signature.
839840 def printing_decorator[F: Callable[... , Any]](func: F) -> F:
@@ -854,7 +855,8 @@ Here is the example using the legacy syntax (Python 3.11 and earlier):
854855
855856.. code-block :: python
856857
857- from typing import Any, Callable, TypeVar, cast
858+ from collections.abc import Callable
859+ from typing import Any, TypeVar, cast
858860
859861 F = TypeVar(' F' , bound = Callable[... , Any])
860862
@@ -887,7 +889,7 @@ for a more faithful type annotation (Python 3.12 syntax):
887889
888890.. code-block :: python
889891
890- from typing import Callable
892+ from collections.abc import Callable
891893
892894 def printing_decorator[** P, T](func: Callable[P, T]) -> Callable[P, T]:
893895 def wrapper (* args : P.args, ** kwds : P.kwargs) -> T:
@@ -900,7 +902,8 @@ The same is possible using the legacy syntax with :py:class:`~typing.ParamSpec`
900902
901903.. code-block :: python
902904
903- from typing import Callable, TypeVar
905+ from collections.abc import Callable
906+ from typing import TypeVar
904907 from typing_extensions import ParamSpec
905908
906909 P = ParamSpec(' P' )
@@ -917,7 +920,7 @@ alter the signature of the input function (Python 3.12 syntax):
917920
918921.. code-block :: python
919922
920- from typing import Callable
923+ from collections.abc import Callable
921924
922925 # We reuse 'P' in the return type, but replace 'T' with 'str'
923926 def stringify[** P, T](func: Callable[P, T]) -> Callable[P, str ]:
@@ -937,7 +940,8 @@ Here is the above example using the legacy syntax (Python 3.11 and earlier):
937940
938941.. code-block :: python
939942
940- from typing import Callable, TypeVar
943+ from collections.abc import Callable
944+ from typing import TypeVar
941945 from typing_extensions import ParamSpec
942946
943947 P = ParamSpec(' P' )
@@ -953,7 +957,8 @@ You can also insert an argument in a decorator (Python 3.12 syntax):
953957
954958.. code-block :: python
955959
956- from typing import Callable, Concatenate
960+ from collections.abc import Callable
961+ from typing import Concatenate
957962
958963 def printing_decorator[** P, T](func: Callable[P, T]) -> Callable[Concatenate[str , P], T]:
959964 def wrapper (msg : str , / , * args : P.args, ** kwds : P.kwargs) -> T:
@@ -971,7 +976,8 @@ Here is the same function using the legacy syntax (Python 3.11 and earlier):
971976
972977.. code-block :: python
973978
974- from typing import Callable, TypeVar
979+ from collections.abc import Callable
980+ from typing import TypeVar
975981 from typing_extensions import Concatenate, ParamSpec
976982
977983 P = ParamSpec(' P' )
@@ -993,7 +999,8 @@ similarly supported via generics (Python 3.12 syntax):
993999
9941000.. code-block :: python
9951001
996- from typing import Any, Callable
1002+ from colletions.abc import Callable
1003+ from typing import Any
9971004
9981005 def route[F: Callable[... , Any]](url: str ) -> Callable[[F], F]:
9991006 ...
@@ -1011,7 +1018,8 @@ Here is the example using the legacy syntax (Python 3.11 and earlier):
10111018
10121019.. code-block :: python
10131020
1014- from typing import Any, Callable, TypeVar
1021+ from collections.abc import Callable
1022+ from typing import Any, TypeVar
10151023
10161024 F = TypeVar(' F' , bound = Callable[... , Any])
10171025
@@ -1027,7 +1035,8 @@ achieved by combining with :py:func:`@overload <typing.overload>` (Python 3.12 s
10271035
10281036.. code-block :: python
10291037
1030- from typing import Any, Callable, overload
1038+ from collections.abc import Callable
1039+ from typing import Any, overload
10311040
10321041 # Bare decorator usage
10331042 @overload
@@ -1057,7 +1066,8 @@ Here is the decorator from the example using the legacy syntax
10571066
10581067.. code-block :: python
10591068
1060- from typing import Any, Callable, Optional, TypeVar, overload
1069+ from collections.abc import Callable
1070+ from typing import Any, Optional, TypeVar, overload
10611071
10621072 F = TypeVar(' F' , bound = Callable[... , Any])
10631073
@@ -1077,7 +1087,7 @@ Generic protocols
10771087
10781088Mypy supports generic protocols (see also :ref: `protocol-types `). Several
10791089:ref: `predefined protocols <predefined_protocols >` are generic, such as
1080- :py:class: `Iterable[T] <typing .Iterable> `, and you can define additional
1090+ :py:class: `Iterable[T] <collections.abc .Iterable> `, and you can define additional
10811091generic protocols. Generic protocols mostly follow the normal rules for
10821092generic classes. Example (Python 3.12 syntax):
10831093
@@ -1200,7 +1210,7 @@ type aliases (it also supports non-generic type aliases):
12001210
12011211.. code-block :: python
12021212
1203- from typing import Iterable, Callable
1213+ from collections.abc import Callable, Iterable
12041214
12051215 type TInt[S] = tuple[int , S]
12061216 type UInt[S] = S | int
0 commit comments