1616
1717import asyncio
1818import concurrent .futures as cf
19- from collections .abc import Callable , Coroutine , Hashable
19+ from collections .abc import Callable , Hashable
2020from functools import partial , wraps
2121
22+ from . import _internal_types as i_types
2223from . import _typings as t
2324from ._paramkey import make_key
2425from .lru import LRU
2526
2627__all__ = ("lrutaskcache" , "taskcache" )
2728
2829
29- # Use below doesn't accept non-task Futures, so can't accept general awaitables
30- type CoroFunc [** P , R ] = Callable [P , Coroutine [t .Any , t .Any , R ]]
31- type TaskFunc [** P , R ] = Callable [P , asyncio .Task [R ]]
32- type TaskCoroFunc [** P , R ] = CoroFunc [P , R ] | TaskFunc [P , R ]
33-
34- type _CT_RET = tuple [tuple [t .Any , ...], dict [str , t .Any ]]
35-
36- #: Note CacheTransformers recieve a tuple (args) and dict(kwargs)
37- #: rather than a ParamSpec of the decorated function.
38- #: Warning: Mutations will impact callsite, return new objects as needed.
39- type CacheTransformer = Callable [[tuple [t .Any , ...], dict [str , t .Any ]], _CT_RET ]
40-
41- # This is the only way to return a generic function not dependent on
42- # type-checker specific behavior where the generic is deferred until application
43- # of the function.
44- TYPE_CHECKING = False
45- if TYPE_CHECKING :
46- from typing import Protocol
47-
48- class Deco (Protocol ):
49- def __call__ [** P , R ](self , c : TaskCoroFunc [P , R ], / ) -> TaskFunc [P , R ]: ...
50-
51- else :
52- # This branch is here for something reasonable to exist at runtime
53- # Not importing typing at runtime
54- type Deco [** P , R ] = Callable [[TaskCoroFunc [P , R ]], TaskFunc [P , R ]]
55-
5630# Non-annotation assignments for transformed functions
5731_WRAP_ASSIGN = ("__module__" , "__name__" , "__qualname__" , "__doc__" )
5832
@@ -70,7 +44,7 @@ class _WrappedSignature[**P, R]:
7044 #: PYUPGRADE: Ensure inspect.signature still accepts this
7145 # as func.__signature__
7246 # Known working: py 3.12.0 - py3.14a6 range inclusive
73- def __init__ (self , f : TaskCoroFunc [P , R ], w : TaskFunc [P , R ]) -> None :
47+ def __init__ (self , f : i_types . TaskCoroFunc [P , R ], w : i_types . TaskFunc [P , R ]) -> None :
7448 self ._f : Callable [..., t .Any ] = f # anotation needed for inspect use below....
7549 self ._w = w
7650 self ._sig : t .Any | None = None
@@ -118,8 +92,8 @@ async def _await[R](fut: asyncio.Future[R]) -> R:
11892def taskcache (
11993 ttl : float | None = None ,
12094 * ,
121- cache_transform : CacheTransformer | None = None ,
122- ) -> Deco :
95+ cache_transform : i_types . CacheTransformer | None = None ,
96+ ) -> i_types . TaskCacheDeco :
12397 """Cache the results of the decorated coroutine.
12498
12599 Decorator to modify coroutine functions to instead act as functions
@@ -158,7 +132,7 @@ def taskcache(
158132 def key_func (args : tuple [t .Any , ...], kwds : dict [t .Any , t .Any ], / ) -> Hashable :
159133 return make_key (* cache_transform (args , kwds ))
160134
161- def wrapper [** P , R ](coro : TaskCoroFunc [P , R ], / ) -> TaskFunc [P , R ]:
135+ def wrapper [** P , R ](coro : i_types . TaskCoroFunc [P , R ], / ) -> i_types . TaskFunc [P , R ]:
162136 internal_cache : dict [Hashable , cf .Future [R ]] = {}
163137
164138 def _internal_cache_evict (key : Hashable , _ignored_task : object ) -> None :
@@ -197,8 +171,8 @@ def lrutaskcache(
197171 ttl : float | None = None ,
198172 maxsize : int = 1024 ,
199173 * ,
200- cache_transform : CacheTransformer | None = None ,
201- ) -> Deco :
174+ cache_transform : i_types . CacheTransformer | None = None ,
175+ ) -> i_types . TaskCacheDeco :
202176 """Cache the results of the decorated coroutine.
203177
204178 Decorator to modify coroutine functions to instead act as functions
@@ -244,7 +218,7 @@ def lrutaskcache(
244218 def key_func (args : tuple [t .Any , ...], kwds : dict [t .Any , t .Any ], / ) -> Hashable :
245219 return make_key (* cache_transform (args , kwds ))
246220
247- def wrapper [** P , R ](coro : TaskCoroFunc [P , R ], / ) -> TaskFunc [P , R ]:
221+ def wrapper [** P , R ](coro : i_types . TaskCoroFunc [P , R ], / ) -> i_types . TaskFunc [P , R ]:
248222 internal_cache : LRU [Hashable , cf .Future [R ]] = LRU (maxsize )
249223
250224 def _internal_cache_evict (key : Hashable , _ignored_task : object ) -> None :
0 commit comments