44
55from typing_extensions import Literal
66
7- _TypeClassType = TypeVar ('_TypeClassType' , contravariant = True )
7+ _TypeClassType = TypeVar ('_TypeClassType' )
88_ReturnType = TypeVar ('_ReturnType' )
99_CallbackType = TypeVar ('_CallbackType' )
1010_InstanceType = TypeVar ('_InstanceType' )
1111
1212
13- class TypeClass (Generic [_TypeClassType , _ReturnType , _CallbackType ]):
13+ class _TypeClass (Generic [_TypeClassType , _ReturnType , _CallbackType ]):
1414 """
1515 That's how we represent typeclasses.
1616
@@ -20,7 +20,7 @@ class TypeClass(Generic[_TypeClassType, _ReturnType, _CallbackType]):
2020 .. code:: python
2121
2222 >>> from typing import Callable
23- >>> from classes import TypeClass, typeclass
23+ >>> from classes import typeclass
2424 >>> @typeclass
2525 ... def used(instance, other: int) -> int:
2626 ... '''Example typeclass to be used later.'''
@@ -30,7 +30,7 @@ class TypeClass(Generic[_TypeClassType, _ReturnType, _CallbackType]):
3030 ... return instance + other
3131 ...
3232 >>> def accepts_typeclass(
33- ... callback: TypeClass[int, int, Callable[[int, int], int] ],
33+ ... callback: Callable[[int, int], int],
3434 ... ) -> int:
3535 ... return callback(1, 3)
3636 ...
@@ -92,6 +92,13 @@ def __call__(
9292 We don't guarantee the order of types inside groups.
9393 Use correct types, do not rely on our order.
9494
95+ Callbacks
96+ ~~~~~~~~~
97+
98+ Since, we define ``__call__`` method for this class,
99+ it can be used and typechecked everywhere,
100+ where a regular ``Callable`` is expected.
101+
95102 """
96103 instance_type = type (instance )
97104 implementation = self ._instances .get (instance_type , None )
@@ -109,7 +116,7 @@ def __call__(
109116 )
110117
111118 @overload
112- def instance ( # noqa: D102
119+ def instance (
113120 self ,
114121 type_argument : Type [_InstanceType ],
115122 * ,
@@ -121,7 +128,7 @@ def instance( # noqa: D102
121128 ...
122129
123130 @overload
124- def instance ( # noqa: D102
131+ def instance (
125132 self ,
126133 type_argument ,
127134 * ,
@@ -159,7 +166,7 @@ def typeclass(
159166 signature : _CallbackType ,
160167 # By default `_TypeClassType` and `_ReturnType` are `nothing`,
161168 # but we enhance them via mypy plugin later:
162- ) -> TypeClass [_TypeClassType , _ReturnType , _CallbackType ]:
169+ ) -> _TypeClass [_TypeClassType , _ReturnType , _CallbackType ]:
163170 """
164171 Function to define typeclasses.
165172
@@ -318,4 +325,4 @@ def typeclass(
318325 Remember, that generic protocols have the same limitation as generic types.
319326
320327 """
321- return TypeClass (signature )
328+ return _TypeClass (signature )
0 commit comments