ez-a-sync 0.32.29__cp310-cp310-win32.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of ez-a-sync might be problematic. Click here for more details.

Files changed (177) hide show
  1. a_sync/ENVIRONMENT_VARIABLES.py +42 -0
  2. a_sync/__init__.pxd +2 -0
  3. a_sync/__init__.py +145 -0
  4. a_sync/_smart.c +22803 -0
  5. a_sync/_smart.cp310-win32.pyd +0 -0
  6. a_sync/_smart.pxd +2 -0
  7. a_sync/_smart.pyi +202 -0
  8. a_sync/_smart.pyx +674 -0
  9. a_sync/_typing.py +258 -0
  10. a_sync/a_sync/__init__.py +60 -0
  11. a_sync/a_sync/_descriptor.c +20528 -0
  12. a_sync/a_sync/_descriptor.cp310-win32.pyd +0 -0
  13. a_sync/a_sync/_descriptor.pyi +33 -0
  14. a_sync/a_sync/_descriptor.pyx +422 -0
  15. a_sync/a_sync/_flags.c +6074 -0
  16. a_sync/a_sync/_flags.cp310-win32.pyd +0 -0
  17. a_sync/a_sync/_flags.pxd +3 -0
  18. a_sync/a_sync/_flags.pyx +92 -0
  19. a_sync/a_sync/_helpers.c +14521 -0
  20. a_sync/a_sync/_helpers.cp310-win32.pyd +0 -0
  21. a_sync/a_sync/_helpers.pxd +3 -0
  22. a_sync/a_sync/_helpers.pyi +10 -0
  23. a_sync/a_sync/_helpers.pyx +167 -0
  24. a_sync/a_sync/_kwargs.c +12194 -0
  25. a_sync/a_sync/_kwargs.cp310-win32.pyd +0 -0
  26. a_sync/a_sync/_kwargs.pxd +2 -0
  27. a_sync/a_sync/_kwargs.pyx +64 -0
  28. a_sync/a_sync/_meta.py +210 -0
  29. a_sync/a_sync/abstract.c +12411 -0
  30. a_sync/a_sync/abstract.cp310-win32.pyd +0 -0
  31. a_sync/a_sync/abstract.pyi +141 -0
  32. a_sync/a_sync/abstract.pyx +221 -0
  33. a_sync/a_sync/base.c +14932 -0
  34. a_sync/a_sync/base.cp310-win32.pyd +0 -0
  35. a_sync/a_sync/base.pyi +60 -0
  36. a_sync/a_sync/base.pyx +271 -0
  37. a_sync/a_sync/config.py +168 -0
  38. a_sync/a_sync/decorator.py +651 -0
  39. a_sync/a_sync/flags.c +5272 -0
  40. a_sync/a_sync/flags.cp310-win32.pyd +0 -0
  41. a_sync/a_sync/flags.pxd +72 -0
  42. a_sync/a_sync/flags.pyi +74 -0
  43. a_sync/a_sync/flags.pyx +72 -0
  44. a_sync/a_sync/function.c +37846 -0
  45. a_sync/a_sync/function.cp310-win32.pyd +0 -0
  46. a_sync/a_sync/function.pxd +28 -0
  47. a_sync/a_sync/function.pyi +571 -0
  48. a_sync/a_sync/function.pyx +1381 -0
  49. a_sync/a_sync/method.c +29774 -0
  50. a_sync/a_sync/method.cp310-win32.pyd +0 -0
  51. a_sync/a_sync/method.pxd +9 -0
  52. a_sync/a_sync/method.pyi +525 -0
  53. a_sync/a_sync/method.pyx +1023 -0
  54. a_sync/a_sync/modifiers/__init__.pxd +1 -0
  55. a_sync/a_sync/modifiers/__init__.py +101 -0
  56. a_sync/a_sync/modifiers/cache/__init__.py +160 -0
  57. a_sync/a_sync/modifiers/cache/memory.py +165 -0
  58. a_sync/a_sync/modifiers/limiter.py +132 -0
  59. a_sync/a_sync/modifiers/manager.c +16149 -0
  60. a_sync/a_sync/modifiers/manager.cp310-win32.pyd +0 -0
  61. a_sync/a_sync/modifiers/manager.pxd +5 -0
  62. a_sync/a_sync/modifiers/manager.pyi +219 -0
  63. a_sync/a_sync/modifiers/manager.pyx +299 -0
  64. a_sync/a_sync/modifiers/semaphores.py +173 -0
  65. a_sync/a_sync/property.c +27260 -0
  66. a_sync/a_sync/property.cp310-win32.pyd +0 -0
  67. a_sync/a_sync/property.pyi +376 -0
  68. a_sync/a_sync/property.pyx +819 -0
  69. a_sync/a_sync/singleton.py +63 -0
  70. a_sync/aliases.py +3 -0
  71. a_sync/async_property/__init__.pxd +1 -0
  72. a_sync/async_property/__init__.py +1 -0
  73. a_sync/async_property/cached.c +20386 -0
  74. a_sync/async_property/cached.cp310-win32.pyd +0 -0
  75. a_sync/async_property/cached.pxd +10 -0
  76. a_sync/async_property/cached.pyi +45 -0
  77. a_sync/async_property/cached.pyx +178 -0
  78. a_sync/async_property/proxy.c +34654 -0
  79. a_sync/async_property/proxy.cp310-win32.pyd +0 -0
  80. a_sync/async_property/proxy.pxd +2 -0
  81. a_sync/async_property/proxy.pyi +124 -0
  82. a_sync/async_property/proxy.pyx +474 -0
  83. a_sync/asyncio/__init__.pxd +6 -0
  84. a_sync/asyncio/__init__.py +164 -0
  85. a_sync/asyncio/as_completed.c +18841 -0
  86. a_sync/asyncio/as_completed.cp310-win32.pyd +0 -0
  87. a_sync/asyncio/as_completed.pxd +8 -0
  88. a_sync/asyncio/as_completed.pyi +109 -0
  89. a_sync/asyncio/as_completed.pyx +269 -0
  90. a_sync/asyncio/create_task.c +15902 -0
  91. a_sync/asyncio/create_task.cp310-win32.pyd +0 -0
  92. a_sync/asyncio/create_task.pxd +2 -0
  93. a_sync/asyncio/create_task.pyi +51 -0
  94. a_sync/asyncio/create_task.pyx +271 -0
  95. a_sync/asyncio/gather.c +16679 -0
  96. a_sync/asyncio/gather.cp310-win32.pyd +0 -0
  97. a_sync/asyncio/gather.pyi +107 -0
  98. a_sync/asyncio/gather.pyx +218 -0
  99. a_sync/asyncio/igather.c +12676 -0
  100. a_sync/asyncio/igather.cp310-win32.pyd +0 -0
  101. a_sync/asyncio/igather.pxd +1 -0
  102. a_sync/asyncio/igather.pyi +7 -0
  103. a_sync/asyncio/igather.pyx +182 -0
  104. a_sync/asyncio/sleep.c +9593 -0
  105. a_sync/asyncio/sleep.cp310-win32.pyd +0 -0
  106. a_sync/asyncio/sleep.pyi +14 -0
  107. a_sync/asyncio/sleep.pyx +49 -0
  108. a_sync/debugging.c +15362 -0
  109. a_sync/debugging.cp310-win32.pyd +0 -0
  110. a_sync/debugging.pyi +76 -0
  111. a_sync/debugging.pyx +107 -0
  112. a_sync/exceptions.c +13312 -0
  113. a_sync/exceptions.cp310-win32.pyd +0 -0
  114. a_sync/exceptions.pyi +376 -0
  115. a_sync/exceptions.pyx +446 -0
  116. a_sync/executor.py +619 -0
  117. a_sync/functools.c +12738 -0
  118. a_sync/functools.cp310-win32.pyd +0 -0
  119. a_sync/functools.pxd +7 -0
  120. a_sync/functools.pyi +33 -0
  121. a_sync/functools.pyx +139 -0
  122. a_sync/future.py +1497 -0
  123. a_sync/iter.c +37271 -0
  124. a_sync/iter.cp310-win32.pyd +0 -0
  125. a_sync/iter.pxd +11 -0
  126. a_sync/iter.pyi +370 -0
  127. a_sync/iter.pyx +981 -0
  128. a_sync/primitives/__init__.pxd +1 -0
  129. a_sync/primitives/__init__.py +53 -0
  130. a_sync/primitives/_debug.c +15757 -0
  131. a_sync/primitives/_debug.cp310-win32.pyd +0 -0
  132. a_sync/primitives/_debug.pxd +12 -0
  133. a_sync/primitives/_debug.pyi +52 -0
  134. a_sync/primitives/_debug.pyx +223 -0
  135. a_sync/primitives/_loggable.c +11529 -0
  136. a_sync/primitives/_loggable.cp310-win32.pyd +0 -0
  137. a_sync/primitives/_loggable.pxd +4 -0
  138. a_sync/primitives/_loggable.pyi +66 -0
  139. a_sync/primitives/_loggable.pyx +102 -0
  140. a_sync/primitives/locks/__init__.pxd +8 -0
  141. a_sync/primitives/locks/__init__.py +17 -0
  142. a_sync/primitives/locks/counter.c +17679 -0
  143. a_sync/primitives/locks/counter.cp310-win32.pyd +0 -0
  144. a_sync/primitives/locks/counter.pxd +12 -0
  145. a_sync/primitives/locks/counter.pyi +151 -0
  146. a_sync/primitives/locks/counter.pyx +260 -0
  147. a_sync/primitives/locks/event.c +17063 -0
  148. a_sync/primitives/locks/event.cp310-win32.pyd +0 -0
  149. a_sync/primitives/locks/event.pxd +22 -0
  150. a_sync/primitives/locks/event.pyi +43 -0
  151. a_sync/primitives/locks/event.pyx +185 -0
  152. a_sync/primitives/locks/prio_semaphore.c +25590 -0
  153. a_sync/primitives/locks/prio_semaphore.cp310-win32.pyd +0 -0
  154. a_sync/primitives/locks/prio_semaphore.pxd +25 -0
  155. a_sync/primitives/locks/prio_semaphore.pyi +217 -0
  156. a_sync/primitives/locks/prio_semaphore.pyx +597 -0
  157. a_sync/primitives/locks/semaphore.c +26509 -0
  158. a_sync/primitives/locks/semaphore.cp310-win32.pyd +0 -0
  159. a_sync/primitives/locks/semaphore.pxd +21 -0
  160. a_sync/primitives/locks/semaphore.pyi +197 -0
  161. a_sync/primitives/locks/semaphore.pyx +454 -0
  162. a_sync/primitives/queue.py +1022 -0
  163. a_sync/py.typed +0 -0
  164. a_sync/sphinx/__init__.py +3 -0
  165. a_sync/sphinx/ext.py +289 -0
  166. a_sync/task.py +932 -0
  167. a_sync/utils/__init__.py +105 -0
  168. a_sync/utils/iterators.py +297 -0
  169. a_sync/utils/repr.c +15799 -0
  170. a_sync/utils/repr.cp310-win32.pyd +0 -0
  171. a_sync/utils/repr.pyi +2 -0
  172. a_sync/utils/repr.pyx +73 -0
  173. ez_a_sync-0.32.29.dist-info/METADATA +367 -0
  174. ez_a_sync-0.32.29.dist-info/RECORD +177 -0
  175. ez_a_sync-0.32.29.dist-info/WHEEL +5 -0
  176. ez_a_sync-0.32.29.dist-info/licenses/LICENSE.txt +17 -0
  177. ez_a_sync-0.32.29.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1381 @@
1
+ import asyncio
2
+ import inspect
3
+ import sys
4
+ import typing
5
+ from logging import getLogger
6
+ from libc.stdint cimport uintptr_t
7
+
8
+ import async_lru
9
+ import async_property
10
+ from typing_extensions import Unpack
11
+
12
+ from a_sync._typing import AnyFn, AnyIterable, CoroFn, DefaultMode, MaybeCoro, ModifierKwargs, P, SyncFn, T
13
+ from a_sync.a_sync._kwargs cimport get_flag_name, is_sync
14
+ from a_sync.a_sync._helpers cimport _asyncify, _await
15
+ from a_sync.a_sync.flags cimport VIABLE_FLAGS
16
+ from a_sync.a_sync.modifiers cimport ModifierManager
17
+ from a_sync.functools cimport update_wrapper, wraps
18
+
19
+ if typing.TYPE_CHECKING:
20
+ from a_sync import TaskMapping
21
+ from a_sync.a_sync.method import (
22
+ ASyncBoundMethod,
23
+ ASyncBoundMethodAsyncDefault,
24
+ ASyncBoundMethodSyncDefault,
25
+ )
26
+
27
+ else:
28
+ # due to circ import issues we will populate this later
29
+ TaskMapping = None
30
+
31
+
32
+ ctypedef object object_id
33
+
34
+
35
+ # cdef asyncio
36
+ cdef object iscoroutinefunction = asyncio.iscoroutinefunction
37
+ del asyncio
38
+
39
+ # cdef inspect
40
+ cdef object getargspec
41
+ if sys.version_info < (3, 11):
42
+ getargspec = inspect.getargspec
43
+ else:
44
+ # getargspec was deprecated in python 3.11
45
+ getargspec = inspect.getfullargspec
46
+ cdef object isasyncgenfunction = inspect.isasyncgenfunction
47
+ cdef object isgeneratorfunction = inspect.isgeneratorfunction
48
+ del inspect
49
+
50
+ # cdef logging
51
+ cdef public object logger = getLogger(__name__)
52
+ cdef object _logger_debug = logger.debug
53
+
54
+ # cdef typing
55
+ cdef object TYPE_CHECKING = typing.TYPE_CHECKING
56
+ cdef object Any = typing.Any
57
+ cdef object Callable = typing.Callable
58
+ cdef object Coroutine = typing.Coroutine
59
+ cdef object Generic = typing.Generic
60
+ cdef object Literal = typing.Literal
61
+ cdef object Optional = typing.Optional
62
+ cdef object overload = typing.overload
63
+
64
+
65
+ # cdef async_lru
66
+ cdef object _LRUCacheWrapper = async_lru._LRUCacheWrapper
67
+
68
+ # cdef async_property
69
+ cdef object AsyncPropertyDescriptor = async_property.base.AsyncPropertyDescriptor
70
+ cdef object AsyncCachedPropertyDescriptor = async_property.cached.AsyncCachedPropertyDescriptor
71
+
72
+
73
+ cdef class _ModifiedMixin:
74
+ """
75
+ A mixin class for internal use that provides functionality for applying modifiers to functions.
76
+
77
+ This class is used as a base for :class:`~ASyncFunction` and its variants, such as
78
+ `ASyncFunctionAsyncDefault` and `ASyncFunctionSyncDefault`, to handle the application
79
+ of async and sync modifiers to functions. Modifiers can alter the behavior of functions,
80
+ such as converting sync functions to async, applying caching, or rate limiting.
81
+
82
+ See Also:
83
+ - :class:`~ASyncFunction`
84
+ - :class:`~ModifierManager`
85
+ """
86
+
87
+ @property
88
+ def default(self) -> DefaultMode:
89
+ """
90
+ Gets the default execution mode (sync, async, or None) for the function.
91
+
92
+ Returns:
93
+ The default execution mode.
94
+
95
+ See Also:
96
+ - :attr:`ModifierManager.default`
97
+ """
98
+ return self.get_default()
99
+
100
+ cdef inline object _asyncify(self, func: SyncFn[P, T]):
101
+ """
102
+ Converts a synchronous function to an asynchronous one and applies async modifiers.
103
+
104
+ Args:
105
+ func: The synchronous function to be converted.
106
+
107
+ Returns:
108
+ The asynchronous version of the function with applied modifiers.
109
+
110
+ See Also:
111
+ - :meth:`ModifierManager.apply_async_modifiers`
112
+ """
113
+ return self.modifiers.apply_async_modifiers(_asyncify(func, self.modifiers.executor))
114
+
115
+ cdef inline object get_await(self):
116
+ """
117
+ Applies sync modifiers to the _helpers._await function and caches it.
118
+
119
+ Returns:
120
+ The modified _await function.
121
+
122
+ See Also:
123
+ - :meth:`ModifierManager.apply_sync_modifiers`
124
+ """
125
+ awaiter = self.__await
126
+ if awaiter is None:
127
+ awaiter = self.modifiers.apply_sync_modifiers(_await)
128
+ self.__await = awaiter
129
+ return awaiter
130
+
131
+ cdef inline str get_default(self):
132
+ cdef str default = self.__default
133
+ if default is None:
134
+ default = self.modifiers.get_default()
135
+ self.__default = default
136
+ return default
137
+
138
+
139
+
140
+ cdef void _validate_wrapped_fn(fn: Callable):
141
+ """Ensures 'fn' is an appropriate function for wrapping with a_sync.
142
+
143
+ Args:
144
+ fn: The function to validate.
145
+
146
+ Raises:
147
+ TypeError: If the input is not callable.
148
+ RuntimeError: If the function has arguments with names that conflict with viable flags.
149
+
150
+ See Also:
151
+ - :func:`_check_not_genfunc`
152
+ """
153
+ typ = type(fn)
154
+ if typ is _function_type:
155
+ _check_not_genfunc_cached(fn)
156
+ _validate_argspec_cached(fn)
157
+ return
158
+ if issubclass(typ, (AsyncPropertyDescriptor, AsyncCachedPropertyDescriptor)):
159
+ return # These are always valid
160
+ elif issubclass(typ, _LRUCacheWrapper):
161
+ fn = fn.__wrapped__
162
+ if type(fn) is _function_type:
163
+ _check_not_genfunc_cached(fn)
164
+ _validate_argspec_cached(fn)
165
+ return
166
+ elif not callable(fn):
167
+ raise TypeError(f"Input is not callable. Unable to decorate {fn}")
168
+ _check_not_genfunc(fn)
169
+ _validate_argspec(fn)
170
+
171
+ cdef object _function_type = type(getLogger)
172
+
173
+ cdef set[object_id] _argspec_validated = set()
174
+
175
+ cdef inline void _validate_argspec_cached(fn: Callable):
176
+ cdef object_id fid = id(fn)
177
+ if fid not in _argspec_validated:
178
+ _validate_argspec(fn)
179
+ _argspec_validated.add(fid)
180
+
181
+ cdef inline void _validate_argspec(fn: Callable):
182
+ cdef tuple[str, ...] fn_args
183
+ cdef object fn_code
184
+
185
+ try:
186
+ fn_code = fn.__code__ # May fail for built-ins or special callables
187
+ fn_args = fn_code.co_varnames[:fn_code.co_argcount]
188
+ except:
189
+ try:
190
+ argspec = getargspec(fn)
191
+ except TypeError:
192
+ warn = logger.warning
193
+ warn(f"inspect.{getargspec.__name__} does not support {fn}")
194
+ warn("we will allow you to proceed but cannot guarantee things will work")
195
+ warn("hopefully you know what you're doing...")
196
+ return
197
+ else:
198
+ # python argspec is already a tuple but mypyc compiled functions
199
+ # return a list which me must coerce to tuple.
200
+ fn_args = tuple(argspec[0])
201
+
202
+ for flag in fn_args:
203
+ if flag in VIABLE_FLAGS:
204
+ raise RuntimeError(
205
+ f"{fn} must not have any arguments with the following names: {VIABLE_FLAGS}"
206
+ )
207
+
208
+
209
+ cdef class _ASyncFunction(_ModifiedMixin):
210
+ """
211
+ A callable wrapper object that can be executed both synchronously and asynchronously.
212
+
213
+ This class wraps a function or coroutine function, allowing it to be called in both
214
+ synchronous and asynchronous contexts. It provides a flexible interface for handling
215
+ different execution modes and applying various modifiers to the function's behavior.
216
+
217
+ The class supports various modifiers that can alter the behavior of the function,
218
+ such as caching, rate limiting, and execution in specific contexts (e.g., thread pools).
219
+
220
+ Note:
221
+ The logic for determining whether to execute the function synchronously or asynchronously
222
+ is handled by the `self.fn` property, which checks for flags in the `kwargs` and defers
223
+ to the default execution mode if no flags are specified.
224
+
225
+ Example:
226
+ async def my_coroutine(x: int) -> str:
227
+ return str(x)
228
+
229
+ func = ASyncFunction(my_coroutine)
230
+
231
+ # Synchronous call
232
+ result = func(5, sync=True) # returns "5"
233
+
234
+ # Asynchronous call
235
+ result = await func(5) # returns "5"
236
+
237
+ See Also:
238
+ - :class:`_ModifiedMixin`
239
+ - :class:`ModifierManager`
240
+ """
241
+
242
+ def __init__(
243
+ self,
244
+ fn: AnyFn[P, T],
245
+ _skip_validate: bint = False,
246
+ **modifiers: Unpack[ModifierKwargs],
247
+ ) -> None:
248
+ """
249
+ Initializes an ASyncFunction instance.
250
+
251
+ Args:
252
+ fn: The function to wrap.
253
+ _skip_validate: For internal use only. Skips validation of the wrapped function when its already been validated once before.
254
+ **modifiers: Keyword arguments for function modifiers.
255
+
256
+ See Also:
257
+ - :func:`_validate_wrapped_fn`
258
+ - :class:`ModifierManager`
259
+ """
260
+ if not _skip_validate:
261
+ _validate_wrapped_fn(fn)
262
+
263
+ self.modifiers = ModifierManager(modifiers)
264
+ """A :class:`~ModifierManager` instance managing function modifiers."""
265
+
266
+ self._fn = None
267
+ """The wrapped callable that will be called. This will be populated the first time it is needed."""
268
+
269
+ self.__wrapped__ = fn
270
+ """The original function that was wrapped."""
271
+
272
+ self.__sync_default_cached = False
273
+ self.__async_def_cached = False
274
+
275
+ def __call__(self, *args: P.args, **kwargs: P.kwargs) -> MaybeCoro[T]:
276
+ """
277
+ Calls the wrapped function either synchronously or asynchronously.
278
+
279
+ This method determines whether to execute the wrapped function synchronously
280
+ or asynchronously based on the default mode and any provided flags. The
281
+ decision logic is encapsulated within the `self.fn` property, which uses
282
+ the `_run_sync` method to decide the execution mode.
283
+
284
+ Note:
285
+ The `self.fn` property is responsible for selecting the appropriate
286
+ execution path (sync or async) by leveraging the `_run_sync` method.
287
+
288
+ Args:
289
+ *args: Positional arguments to pass to the wrapped function.
290
+ **kwargs: Keyword arguments to pass to the wrapped function.
291
+
292
+ Raises:
293
+ Exception: Any exception that may be raised by the wrapped function.
294
+
295
+ See Also:
296
+ - :attr:`default`
297
+ - :meth:`_run_sync`
298
+ """
299
+ fn = self.get_fn()
300
+ _logger_debug(
301
+ "calling %s fn: %s with args: %s kwargs: %s", self, fn, args, kwargs
302
+ )
303
+ return fn(*args, **kwargs)
304
+
305
+ def __repr__(self) -> str:
306
+ return "<{} {}.{} at {}>".format(
307
+ self.__class__.__name__, self.__module__, self.__name__, hex(id(self))
308
+ )
309
+
310
+ @property
311
+ def fn(self):
312
+ # NOTE type hint doesnt work in py3.8 or py3.9, debug later
313
+ # -> Union[SyncFn[[CoroFn[P, T]], MaybeAwaitable[T]], SyncFn[[SyncFn[P, T]], MaybeAwaitable[T]]]:
314
+ """
315
+ Returns the final wrapped version of :attr:`ASyncFunction.__wrapped__` decorated with all of the a_sync goodness.
316
+
317
+ Returns:
318
+ The final wrapped function.
319
+
320
+ See Also:
321
+ - :meth:`_async_wrap`
322
+ - :meth:`_sync_wrap`
323
+ """
324
+ return self.get_fn()
325
+
326
+ cdef object get_fn(self):
327
+ fn = self._fn
328
+ if fn is None:
329
+ fn = self._async_wrap if self.is_async_def() else self._sync_wrap
330
+ self._fn = fn
331
+ return fn
332
+
333
+ if sys.version_info >= (3, 11) or TYPE_CHECKING:
334
+ # we can specify P.args in python>=3.11 but in lower versions it causes a crash. Everything should still type check correctly on all versions.
335
+ def map(
336
+ self,
337
+ *iterables: AnyIterable[P.args],
338
+ concurrency: Optional[int] = None,
339
+ task_name: str = "",
340
+ **function_kwargs: P.kwargs,
341
+ ) -> "TaskMapping[P, T]":
342
+ """
343
+ Creates a TaskMapping for the wrapped function with the given iterables.
344
+
345
+ Args:
346
+ *iterables: Iterable objects to be used as arguments for the function.
347
+ concurrency: Optional maximum number of concurrent tasks.
348
+ task_name: Optional name for the tasks.
349
+ **function_kwargs: Additional keyword arguments to pass to the function.
350
+
351
+ Returns:
352
+ A TaskMapping object for managing concurrent execution.
353
+
354
+ See Also:
355
+ - :class:`TaskMapping`
356
+ """
357
+ if TaskMapping is None:
358
+ _import_TaskMapping()
359
+
360
+ return TaskMapping(
361
+ self,
362
+ *iterables,
363
+ concurrency=concurrency,
364
+ name=task_name,
365
+ **function_kwargs,
366
+ )
367
+
368
+ async def any(
369
+ self,
370
+ *iterables: AnyIterable[P.args],
371
+ concurrency: Optional[int] = None,
372
+ task_name: str = "",
373
+ **function_kwargs: P.kwargs,
374
+ ) -> bint:
375
+ """
376
+ Checks if any result of the function applied to the iterables is truthy.
377
+
378
+ Args:
379
+ *iterables: Iterable objects to be used as arguments for the function.
380
+ concurrency: Optional maximum number of concurrent tasks.
381
+ task_name: Optional name for the tasks.
382
+ **function_kwargs: Additional keyword arguments to pass to the function.
383
+
384
+ Returns:
385
+ True if any result is truthy, otherwise False.
386
+
387
+ See Also:
388
+ - :meth:`map`
389
+ """
390
+ return await self.map(
391
+ *iterables,
392
+ concurrency=concurrency,
393
+ task_name=task_name,
394
+ **function_kwargs,
395
+ ).any(pop=True, sync=False)
396
+
397
+ async def all(
398
+ self,
399
+ *iterables: AnyIterable[P.args],
400
+ concurrency: Optional[int] = None,
401
+ task_name: str = "",
402
+ **function_kwargs: P.kwargs,
403
+ ) -> bint:
404
+ """
405
+ Checks if all results of the function applied to the iterables are truthy.
406
+
407
+ Args:
408
+ *iterables: Iterable objects to be used as arguments for the function.
409
+ concurrency: Optional maximum number of concurrent tasks.
410
+ task_name: Optional name for the tasks.
411
+ **function_kwargs: Additional keyword arguments to pass to the function.
412
+
413
+ Returns:
414
+ True if all results are truthy, otherwise False.
415
+
416
+ See Also:
417
+ - :meth:`map`
418
+ """
419
+ return await self.map(
420
+ *iterables,
421
+ concurrency=concurrency,
422
+ task_name=task_name,
423
+ **function_kwargs,
424
+ ).all(pop=True, sync=False)
425
+
426
+ async def min(
427
+ self,
428
+ *iterables: AnyIterable[P.args],
429
+ concurrency: Optional[int] = None,
430
+ task_name: str = "",
431
+ **function_kwargs: P.kwargs,
432
+ ) -> T:
433
+ """
434
+ Finds the minimum result of the function applied to the iterables.
435
+
436
+ Args:
437
+ *iterables: Iterable objects to be used as arguments for the function.
438
+ concurrency: Optional maximum number of concurrent tasks.
439
+ task_name: Optional name for the tasks.
440
+ **function_kwargs: Additional keyword arguments to pass to the function.
441
+
442
+ Returns:
443
+ The minimum result.
444
+
445
+ See Also:
446
+ - :meth:`map`
447
+ """
448
+ return await self.map(
449
+ *iterables,
450
+ concurrency=concurrency,
451
+ task_name=task_name,
452
+ **function_kwargs,
453
+ ).min(pop=True, sync=False)
454
+
455
+ async def max(
456
+ self,
457
+ *iterables: AnyIterable[P.args],
458
+ concurrency: Optional[int] = None,
459
+ task_name: str = "",
460
+ **function_kwargs: P.kwargs,
461
+ ) -> T:
462
+ """
463
+ Finds the maximum result of the function applied to the iterables.
464
+
465
+ Args:
466
+ *iterables: Iterable objects to be used as arguments for the function.
467
+ concurrency: Optional maximum number of concurrent tasks.
468
+ task_name: Optional name for the tasks.
469
+ **function_kwargs: Additional keyword arguments to pass to the function.
470
+
471
+ Returns:
472
+ The maximum result.
473
+
474
+ See Also:
475
+ - :meth:`map`
476
+ """
477
+ return await self.map(
478
+ *iterables,
479
+ concurrency=concurrency,
480
+ task_name=task_name,
481
+ **function_kwargs,
482
+ ).max(pop=True, sync=False)
483
+
484
+ async def sum(
485
+ self,
486
+ *iterables: AnyIterable[P.args],
487
+ concurrency: Optional[int] = None,
488
+ task_name: str = "",
489
+ **function_kwargs: P.kwargs,
490
+ ) -> T:
491
+ """
492
+ Calculates the sum of the results of the function applied to the iterables.
493
+
494
+ Args:
495
+ *iterables: Iterable objects to be used as arguments for the function.
496
+ concurrency: Optional maximum number of concurrent tasks.
497
+ task_name: Optional name for the tasks.
498
+ **function_kwargs: Additional keyword arguments to pass to the function.
499
+
500
+ Returns:
501
+ The sum of the results.
502
+
503
+ See Also:
504
+ - :meth:`map`
505
+ """
506
+ return await self.map(
507
+ *iterables,
508
+ concurrency=concurrency,
509
+ task_name=task_name,
510
+ **function_kwargs,
511
+ ).sum(pop=True, sync=False)
512
+
513
+ else:
514
+
515
+ def map(
516
+ self,
517
+ *iterables: AnyIterable[Any],
518
+ concurrency: Optional[int] = None,
519
+ task_name: str = "",
520
+ **function_kwargs: P.kwargs,
521
+ ) -> "TaskMapping[P, T]":
522
+ """
523
+ Creates a TaskMapping for the wrapped function with the given iterables.
524
+
525
+ Args:
526
+ *iterables: Iterable objects to be used as arguments for the function.
527
+ concurrency: Optional maximum number of concurrent tasks.
528
+ task_name: Optional name for the tasks.
529
+ **function_kwargs: Additional keyword arguments to pass to the function.
530
+
531
+ Returns:
532
+ A TaskMapping object for managing concurrent execution.
533
+
534
+ See Also:
535
+ - :class:`TaskMapping`
536
+ """
537
+ if TaskMapping is None:
538
+ _import_TaskMapping()
539
+
540
+ return TaskMapping(
541
+ self,
542
+ *iterables,
543
+ concurrency=concurrency,
544
+ name=task_name,
545
+ **function_kwargs,
546
+ )
547
+
548
+ async def any(
549
+ self,
550
+ *iterables: AnyIterable[Any],
551
+ concurrency: Optional[int] = None,
552
+ task_name: str = "",
553
+ **function_kwargs: P.kwargs,
554
+ ) -> bint:
555
+ """
556
+ Checks if any result of the function applied to the iterables is truthy.
557
+
558
+ Args:
559
+ *iterables: Iterable objects to be used as arguments for the function.
560
+ concurrency: Optional maximum number of concurrent tasks.
561
+ task_name: Optional name for the tasks.
562
+ **function_kwargs: Additional keyword arguments to pass to the function.
563
+
564
+ Returns:
565
+ True if any result is truthy, otherwise False.
566
+
567
+ See Also:
568
+ - :meth:`map`
569
+ """
570
+ return await self.map(
571
+ *iterables,
572
+ concurrency=concurrency,
573
+ task_name=task_name,
574
+ **function_kwargs,
575
+ ).any(pop=True, sync=False)
576
+
577
+ async def all(
578
+ self,
579
+ *iterables: AnyIterable[Any],
580
+ concurrency: Optional[int] = None,
581
+ task_name: str = "",
582
+ **function_kwargs: P.kwargs,
583
+ ) -> bint:
584
+ """
585
+ Checks if all results of the function applied to the iterables are truthy.
586
+
587
+ Args:
588
+ *iterables: Iterable objects to be used as arguments for the function.
589
+ concurrency: Optional maximum number of concurrent tasks.
590
+ task_name: Optional name for the tasks.
591
+ **function_kwargs: Additional keyword arguments to pass to the function.
592
+
593
+ Returns:
594
+ True if all results are truthy, otherwise False.
595
+
596
+ See Also:
597
+ - :meth:`map`
598
+ """
599
+ return await self.map(
600
+ *iterables,
601
+ concurrency=concurrency,
602
+ task_name=task_name,
603
+ **function_kwargs,
604
+ ).all(pop=True, sync=False)
605
+
606
+ async def min(
607
+ self,
608
+ *iterables: AnyIterable[Any],
609
+ concurrency: Optional[int] = None,
610
+ task_name: str = "",
611
+ **function_kwargs: P.kwargs,
612
+ ) -> T:
613
+ """
614
+ Finds the minimum result of the function applied to the iterables.
615
+
616
+ Args:
617
+ *iterables: Iterable objects to be used as arguments for the function.
618
+ concurrency: Optional maximum number of concurrent tasks.
619
+ task_name: Optional name for the tasks.
620
+ **function_kwargs: Additional keyword arguments to pass to the function.
621
+
622
+ Returns:
623
+ The minimum result.
624
+
625
+ See Also:
626
+ - :meth:`map`
627
+ """
628
+ return await self.map(
629
+ *iterables,
630
+ concurrency=concurrency,
631
+ task_name=task_name,
632
+ **function_kwargs,
633
+ ).min(pop=True, sync=False)
634
+
635
+ async def max(
636
+ self,
637
+ *iterables: AnyIterable[Any],
638
+ concurrency: Optional[int] = None,
639
+ task_name: str = "",
640
+ **function_kwargs: P.kwargs,
641
+ ) -> T:
642
+ """
643
+ Finds the maximum result of the function applied to the iterables.
644
+
645
+ Args:
646
+ *iterables: Iterable objects to be used as arguments for the function.
647
+ concurrency: Optional maximum number of concurrent tasks.
648
+ task_name: Optional name for the tasks.
649
+ **function_kwargs: Additional keyword arguments to pass to the function.
650
+
651
+ Returns:
652
+ The maximum result.
653
+
654
+ See Also:
655
+ - :meth:`map`
656
+ """
657
+ return await self.map(
658
+ *iterables,
659
+ concurrency=concurrency,
660
+ task_name=task_name,
661
+ **function_kwargs,
662
+ ).max(pop=True, sync=False)
663
+
664
+ async def sum(
665
+ self,
666
+ *iterables: AnyIterable[Any],
667
+ concurrency: Optional[int] = None,
668
+ task_name: str = "",
669
+ **function_kwargs: P.kwargs,
670
+ ) -> T:
671
+ """
672
+ Calculates the sum of the results of the function applied to the iterables.
673
+
674
+ Args:
675
+ *iterables: Iterable objects to be used as arguments for the function.
676
+ concurrency: Optional maximum number of concurrent tasks.
677
+ task_name: Optional name for the tasks.
678
+ **function_kwargs: Additional keyword arguments to pass to the function.
679
+
680
+ Returns:
681
+ The sum of the results.
682
+
683
+ See Also:
684
+ - :meth:`map`
685
+ """
686
+ return await self.map(
687
+ *iterables,
688
+ concurrency=concurrency,
689
+ task_name=task_name,
690
+ **function_kwargs,
691
+ ).sum(pop=True, sync=False)
692
+
693
+ @property
694
+ def _asyncified(self) -> CoroFn[P, T]:
695
+ """
696
+ Converts the wrapped function to an asynchronous function and applies both sync and async modifiers.
697
+
698
+ Raises:
699
+ TypeError: If the wrapped function is already asynchronous.
700
+
701
+ Returns:
702
+ The asynchronous version of the wrapped function.
703
+
704
+ See Also:
705
+ - :meth:`_asyncify`
706
+ """
707
+ asyncified = self.__asyncified
708
+ if asyncified is None:
709
+ if self.is_async_def():
710
+ raise TypeError(
711
+ f"Can only be applied to sync functions, not {self.__wrapped__}"
712
+ )
713
+
714
+ asyncified = self.__asyncified = self._asyncify(self._modified_fn)
715
+ return asyncified
716
+
717
+ @property
718
+ def _modified_fn(self) -> AnyFn[P, T]:
719
+ """
720
+ Applies modifiers to the wrapped function.
721
+
722
+ If the wrapped function is an asynchronous function, this method applies async modifiers.
723
+ If the wrapped function is a synchronous function, this method applies sync modifiers.
724
+
725
+ Returns:
726
+ The modified function.
727
+
728
+ See Also:
729
+ - :meth:`ModifierManager.apply_async_modifiers`
730
+ - :meth:`ModifierManager.apply_sync_modifiers`
731
+ """
732
+ modified_fn = self.__modified_fn
733
+ if modified_fn is None:
734
+
735
+ # recursively unwrap ASync objects to get to the original wrapped callable
736
+ wrapped = self.__wrapped__
737
+ while isinstance(wrapped, _ASyncFunction):
738
+ wrapped = wrapped.__wrapped__
739
+
740
+ if self.is_async_def():
741
+ modified_fn = self.__modified_fn = self.modifiers.apply_async_modifiers(wrapped)
742
+ else:
743
+ modified_fn = self.__modified_fn = self.modifiers.apply_sync_modifiers(wrapped)
744
+
745
+ return modified_fn
746
+
747
+ @property
748
+ def _async_wrap(self): # -> SyncFn[[CoroFn[P, T]], MaybeAwaitable[T]]:
749
+ """
750
+ The final wrapper if the wrapped function is an asynchronous function.
751
+
752
+ This method applies the appropriate modifiers and determines whether to await the result.
753
+
754
+ Returns:
755
+ The wrapped function with async handling.
756
+
757
+ See Also:
758
+ - :meth:`_run_sync`
759
+ - :meth:`_await`
760
+ """
761
+ async_wrap = self.__async_wrap
762
+ if async_wrap is None:
763
+
764
+ modified_fn = self._modified_fn
765
+ await_helper = self.get_await()
766
+
767
+ @wraps(modified_fn)
768
+ def async_wrap(*args: P.args, **kwargs: P.kwargs) -> MaybeAwaitable[T]: # type: ignore [name-defined]
769
+ # sourcery skip: assign-if-exp
770
+ # we dont want this so profiler outputs are more useful
771
+
772
+ # Must take place before coro is created, we're popping a kwarg.
773
+ should_await = self._run_sync(kwargs)
774
+ coro = modified_fn(*args, **kwargs)
775
+ if should_await:
776
+ return await_helper(coro)
777
+ else:
778
+ return coro
779
+
780
+ self.__async_wrap = async_wrap
781
+
782
+ return async_wrap
783
+
784
+ @property
785
+ def _sync_wrap(self): # -> SyncFn[[SyncFn[P, T]], MaybeAwaitable[T]]:
786
+ """
787
+ The final wrapper if the wrapped function is a synchronous function.
788
+
789
+ This method applies the appropriate modifiers and determines whether to run the function synchronously or asynchronously.
790
+
791
+ Returns:
792
+ The wrapped function with sync handling.
793
+
794
+ See Also:
795
+ - :meth:`_run_sync`
796
+ - :meth:`_asyncified`
797
+ """
798
+ sync_wrap = self.__sync_wrap
799
+ if sync_wrap is None:
800
+
801
+ modified_fn = self._modified_fn
802
+ asyncified = self._asyncified
803
+
804
+ @wraps(modified_fn)
805
+ def sync_wrap(*args: P.args, **kwargs: P.kwargs) -> MaybeAwaitable[T]: # type: ignore [name-defined]
806
+ if self._run_sync(kwargs):
807
+ return modified_fn(*args, **kwargs)
808
+ return asyncified(*args, **kwargs)
809
+
810
+ self.__sync_wrap = sync_wrap
811
+
812
+ return sync_wrap
813
+
814
+ cpdef bint is_async_def(self):
815
+ """
816
+ Checks if the wrapped function is an asynchronous function.
817
+
818
+ Returns:
819
+ True if the function is asynchronous, otherwise False.
820
+
821
+ See Also:
822
+ - :func:`asyncio.iscoroutinefunction`
823
+ """
824
+ if self.__async_def_cached:
825
+ return self.__async_def
826
+ cdef bint async_def
827
+
828
+ # recursively unwrap ASync callables to get the original wrapped fn
829
+ wrapped = self.__wrapped__
830
+ while isinstance(wrapped, _ASyncFunction):
831
+ wrapped = wrapped.__wrapped__
832
+
833
+ async_def = self.__async_def = iscoroutinefunction(wrapped)
834
+ self.__async_def_cached = True
835
+ return async_def
836
+
837
+ cpdef bint is_sync_default(self):
838
+ """
839
+ Determines the default execution mode (sync or async) for the function.
840
+
841
+ If the user did not specify a default, this method defers to the function's
842
+ definition (sync vs async def).
843
+
844
+ Returns:
845
+ True if the default is sync, False if async.
846
+
847
+ See Also:
848
+ - :attr:`default`
849
+ """
850
+ if self.__sync_default_cached:
851
+ return self.__sync_default
852
+
853
+ cdef str default = self.get_default()
854
+ cdef bint sync_default = (
855
+ True
856
+ if default == "sync"
857
+ else False if default == "async" else not self.is_async_def()
858
+ )
859
+ self.__sync_default = sync_default
860
+ self.__sync_default_cached = True
861
+ return sync_default
862
+
863
+ cdef inline bint _run_sync(self, dict kwargs):
864
+ """
865
+ Determines whether to run the function synchronously or asynchronously.
866
+
867
+ This method checks for a flag in the kwargs and defers to it if present.
868
+ If no flag is specified, it defers to the default execution mode.
869
+
870
+ Args:
871
+ kwargs: The keyword arguments passed to the function.
872
+
873
+ Returns:
874
+ True if the function should run synchronously, otherwise False.
875
+
876
+ See Also:
877
+ - :func:`_kwargs.get_flag_name`
878
+ """
879
+ cdef str flag = get_flag_name(kwargs)
880
+ if flag:
881
+ # If a flag was specified in the kwargs, we will defer to it.
882
+ return is_sync(flag, kwargs, pop_flag=True)
883
+ else:
884
+ # No flag specified in the kwargs, we will defer to 'default'.
885
+ return self.is_sync_default()
886
+
887
+
888
+ class ASyncFunction(_ASyncFunction, Generic[P, T]):
889
+
890
+ @overload
891
+ def __init__(self, fn: CoroFn[P, T], **modifiers: Unpack[ModifierKwargs]) -> None:
892
+ """
893
+ Initializes an ASyncFunction instance for a coroutine function.
894
+
895
+ Args:
896
+ fn: The coroutine function to wrap.
897
+ **modifiers: Keyword arguments for function modifiers.
898
+
899
+ Example:
900
+ async def my_coroutine(x: int) -> str:
901
+ return str(x)
902
+
903
+ func = ASyncFunction(my_coroutine, cache_type='memory')
904
+ """
905
+
906
+ @overload
907
+ def __init__(self, fn: SyncFn[P, T], **modifiers: Unpack[ModifierKwargs]) -> None:
908
+ """
909
+ Initializes an ASyncFunction instance for a synchronous function.
910
+
911
+ Args:
912
+ fn: The synchronous function to wrap.
913
+ **modifiers: Keyword arguments for function modifiers.
914
+
915
+ Example:
916
+ def my_function(x: int) -> str:
917
+ return str(x)
918
+
919
+ func = ASyncFunction(my_function, runs_per_minute=60)
920
+ """
921
+
922
+ def __init__(
923
+ self,
924
+ fn: AnyFn[P, T],
925
+ _skip_validate: bint = False,
926
+ **modifiers: Unpack[ModifierKwargs],
927
+ ) -> None:
928
+ _ASyncFunction.__init__(self, fn, _skip_validate=_skip_validate, **modifiers)
929
+ update_wrapper(self, fn)
930
+ if self.__doc__ is None:
931
+ self.__doc__ = f"Since `{self.__name__}` is an {self.__docstring_append__}"
932
+ else:
933
+ self.__doc__ += (
934
+ f"\n\nSince `{self.__name__}` is an {self.__docstring_append__}"
935
+ )
936
+
937
+ __docstring_append__ = ":class:`~a_sync.a_sync.function.ASyncFunction`, you can optionally pass either a `sync` or `asynchronous` kwarg with a boolean value."
938
+
939
+ @overload
940
+ def __call__(self, *args: P.args, sync: Literal[True], **kwargs: P.kwargs) -> T:
941
+ """
942
+ Calls the wrapped function synchronously.
943
+
944
+ Args:
945
+ *args: Positional arguments to pass to the wrapped function.
946
+ sync: Must be True to indicate synchronous execution.
947
+ **kwargs: Keyword arguments to pass to the wrapped function.
948
+
949
+ Example:
950
+ result = func(5, sync=True)
951
+ """
952
+
953
+ @overload
954
+ def __call__(
955
+ self, *args: P.args, sync: Literal[False], **kwargs: P.kwargs
956
+ ) -> Coroutine[Any, Any, T]:
957
+ """
958
+ Calls the wrapped function asynchronously.
959
+
960
+ Args:
961
+ *args: Positional arguments to pass to the wrapped function.
962
+ sync: Must be False to indicate asynchronous execution.
963
+ **kwargs: Keyword arguments to pass to the wrapped function.
964
+
965
+ Example:
966
+ result = await func(5, sync=False)
967
+ """
968
+
969
+ @overload
970
+ def __call__(
971
+ self, *args: P.args, asynchronous: Literal[False], **kwargs: P.kwargs
972
+ ) -> T:
973
+ """
974
+ Calls the wrapped function synchronously.
975
+
976
+ Args:
977
+ *args: Positional arguments to pass to the wrapped function.
978
+ asynchronous: Must be False to indicate synchronous execution.
979
+ **kwargs: Keyword arguments to pass to the wrapped function.
980
+
981
+ Example:
982
+ result = func(5, asynchronous=False)
983
+ """
984
+
985
+ @overload
986
+ def __call__(
987
+ self, *args: P.args, asynchronous: Literal[True], **kwargs: P.kwargs
988
+ ) -> Coroutine[Any, Any, T]:
989
+ """
990
+ Calls the wrapped function asynchronously.
991
+
992
+ Args:
993
+ *args: Positional arguments to pass to the wrapped function.
994
+ asynchronous: Must be True to indicate asynchronous execution.
995
+ **kwargs: Keyword arguments to pass to the wrapped function.
996
+
997
+ Example:
998
+ result = await func(5, asynchronous=True)
999
+ """
1000
+
1001
+ @overload
1002
+ def __call__(self, *args: P.args, **kwargs: P.kwargs) -> MaybeCoro[T]:
1003
+ """
1004
+ Calls the wrapped function using the default execution mode.
1005
+
1006
+ Args:
1007
+ *args: Positional arguments to pass to the wrapped function.
1008
+ **kwargs: Keyword arguments to pass to the wrapped function.
1009
+
1010
+ Example:
1011
+ result = func(5)
1012
+ """
1013
+
1014
+
1015
+ if sys.version_info < (3, 10):
1016
+ _inherit = ASyncFunction[AnyFn[P, T], ASyncFunction[P, T]]
1017
+ else:
1018
+ _inherit = ASyncFunction[[AnyFn[P, T]], ASyncFunction[P, T]]
1019
+
1020
+
1021
+ cdef class ASyncDecorator(_ModifiedMixin):
1022
+ def __cinit__(self, **modifiers: Unpack[ModifierKwargs]) -> None:
1023
+ """
1024
+ Initializes an ASyncDecorator instance by validating the inputs.
1025
+
1026
+ Args:
1027
+ **modifiers: Keyword arguments for function modifiers.
1028
+
1029
+ Raises:
1030
+ ValueError: If 'default' is not 'sync', 'async', or None.
1031
+
1032
+ See Also:
1033
+ - :class:`ModifierManager`
1034
+ """
1035
+ if modifiers.get("default", object) not in ("sync", "async", None):
1036
+ if "default" not in modifiers:
1037
+ raise ValueError("you must pass a value for `default`")
1038
+ raise ValueError(
1039
+ f"'default' must be either 'sync', 'async', or None. You passed {modifiers['default']}."
1040
+ )
1041
+ self.modifiers = ModifierManager(modifiers)
1042
+
1043
+ @overload
1044
+ def __call__(self, func: AnyFn[Concatenate[B, P], T]) -> "ASyncBoundMethod[B, P, T]": # type: ignore [override]
1045
+ """
1046
+ Decorates a bound method with async or sync behavior based on the default modifier.
1047
+
1048
+ Args:
1049
+ func: The bound method to decorate.
1050
+
1051
+ Returns:
1052
+ An ASyncBoundMethod instance with the appropriate default behavior.
1053
+
1054
+ See Also:
1055
+ - :class:`ASyncBoundMethod`
1056
+ """
1057
+
1058
+ @overload
1059
+ def __call__(self, func: AnyFn[P, T]) -> ASyncFunction[P, T]: # type: ignore [override]
1060
+ """
1061
+ Decorates a function with async or sync behavior based on the default modifier.
1062
+
1063
+ Args:
1064
+ func: The function to decorate.
1065
+
1066
+ Returns:
1067
+ An ASyncFunction instance with the appropriate default behavior.
1068
+
1069
+ See Also:
1070
+ - :class:`ASyncFunction`
1071
+ """
1072
+
1073
+ def __call__(self, func: AnyFn[P, T]) -> ASyncFunction[P, T]: # type: ignore [override]
1074
+ """
1075
+ Decorates a function with async or sync behavior based on the default modifier.
1076
+
1077
+ Args:
1078
+ func: The function to decorate.
1079
+
1080
+ Returns:
1081
+ An ASyncFunction instance with the appropriate default behavior.
1082
+
1083
+ See Also:
1084
+ - :class:`ASyncFunction`
1085
+ """
1086
+ cdef str default = self.get_default()
1087
+ if default == "async":
1088
+ return ASyncFunctionAsyncDefault(func, **self.modifiers._modifiers)
1089
+ elif default == "sync":
1090
+ return ASyncFunctionSyncDefault(func, **self.modifiers._modifiers)
1091
+ elif iscoroutinefunction(func):
1092
+ return ASyncFunctionAsyncDefault(func, **self.modifiers._modifiers)
1093
+ else:
1094
+ return ASyncFunctionSyncDefault(func, **self.modifiers._modifiers)
1095
+
1096
+
1097
+ cdef set[uintptr_t] _is_genfunc_cache = set()
1098
+
1099
+ cdef void _check_not_genfunc_cached(func: Callable):
1100
+ cdef uintptr_t fid = id(func)
1101
+ if fid not in _is_genfunc_cache:
1102
+ _check_not_genfunc(func)
1103
+ _is_genfunc_cache.add(fid)
1104
+
1105
+ cdef void _check_not_genfunc(func: Callable):
1106
+ """Raises an error if the function is a generator or async generator.
1107
+
1108
+ Args:
1109
+ func: The function to check.
1110
+
1111
+ Raises:
1112
+ ValueError: If the function is a generator or async generator.
1113
+
1114
+ See Also:
1115
+ - :func:`inspect.isasyncgenfunction`
1116
+ - :func:`inspect.isgeneratorfunction`
1117
+ """
1118
+ if isasyncgenfunction(func) or isgeneratorfunction(func):
1119
+ raise ValueError("unable to decorate generator functions with this decorator")
1120
+
1121
+
1122
+ # Mypy helper classes
1123
+
1124
+
1125
+ class ASyncFunctionSyncDefault(ASyncFunction[P, T]):
1126
+ """A specialized :class:`~ASyncFunction` that defaults to synchronous execution.
1127
+
1128
+ This class is used when the :func:`~a_sync` decorator is applied with `default='sync'`.
1129
+ It provides type hints to indicate that the default call behavior is synchronous and
1130
+ supports IDE type checking for most use cases.
1131
+
1132
+ The wrapped function can still be called asynchronously by passing `sync=False`
1133
+ or `asynchronous=True` as a keyword argument.
1134
+
1135
+ Example:
1136
+ @a_sync(default='sync')
1137
+ async def my_function(x: int) -> str:
1138
+ return str(x)
1139
+
1140
+ # Synchronous call (default behavior)
1141
+ result = my_function(5) # returns "5"
1142
+
1143
+ # Asynchronous call
1144
+ result = await my_function(5, sync=False) # returns "5"
1145
+ """
1146
+
1147
+ @overload
1148
+ def __call__(self, *args: P.args, sync: Literal[True], **kwargs: P.kwargs) -> T:
1149
+ # TODO write specific docs for this overload
1150
+ ...
1151
+
1152
+ @overload
1153
+ def __call__(
1154
+ self, *args: P.args, sync: Literal[False], **kwargs: P.kwargs
1155
+ ) -> Coroutine[Any, Any, T]:
1156
+ # TODO write specific docs for this overload
1157
+ ...
1158
+
1159
+ @overload
1160
+ def __call__(
1161
+ self, *args: P.args, asynchronous: Literal[False], **kwargs: P.kwargs
1162
+ ) -> T:
1163
+ # TODO write specific docs for this overload
1164
+ ...
1165
+
1166
+ @overload
1167
+ def __call__(
1168
+ self, *args: P.args, asynchronous: Literal[True], **kwargs: P.kwargs
1169
+ ) -> Coroutine[Any, Any, T]:
1170
+ # TODO write specific docs for this overload
1171
+ ...
1172
+
1173
+ @overload
1174
+ def __call__(self, *args: P.args, **kwargs: P.kwargs) -> T:
1175
+ # TODO write specific docs for this overload
1176
+ ...
1177
+
1178
+ def __call__(_ASyncFunction self, *args: P.args, **kwargs: P.kwargs) -> MaybeCoro[T]:
1179
+ """Calls the wrapped function, defaulting to synchronous execution.
1180
+
1181
+ This method overrides the base :meth:`ASyncFunction.__call__` to provide a synchronous
1182
+ default behavior.
1183
+
1184
+ Args:
1185
+ *args: Positional arguments to pass to the wrapped function.
1186
+ **kwargs: Keyword arguments to pass to the wrapped function.
1187
+
1188
+ Raises:
1189
+ Exception: Any exception that may be raised by the wrapped function.
1190
+
1191
+ Returns:
1192
+ The result of the function call.
1193
+
1194
+ See Also:
1195
+ - :meth:`ASyncFunction.__call__`
1196
+ """
1197
+ return self.get_fn()(*args, **kwargs)
1198
+
1199
+ __docstring_append__ = ":class:`~a_sync.a_sync.function.ASyncFunctionSyncDefault`, you can optionally pass `sync=False` or `asynchronous=True` to force it to return a coroutine. Without either kwarg, it will run synchronously."
1200
+
1201
+
1202
+ class ASyncFunctionAsyncDefault(ASyncFunction[P, T]):
1203
+ """
1204
+ A specialized :class:`~ASyncFunction` that defaults to asynchronous execution.
1205
+
1206
+ This class is used when the :func:`~a_sync` decorator is applied with `default='async'`.
1207
+ It provides type hints to indicate that the default call behavior is asynchronous
1208
+ and supports IDE type checking for most use cases.
1209
+
1210
+ The wrapped function can still be called synchronously by passing `sync=True`
1211
+ or `asynchronous=False` as a keyword argument.
1212
+
1213
+ Example:
1214
+ @a_sync(default='async')
1215
+ async def my_function(x: int) -> str:
1216
+ return str(x)
1217
+
1218
+ # Asynchronous call (default behavior)
1219
+ result = await my_function(5) # returns "5"
1220
+
1221
+ # Synchronous call
1222
+ result = my_function(5, sync=True) # returns "5"
1223
+ """
1224
+
1225
+ @overload
1226
+ def __call__(self, *args: P.args, sync: Literal[True], **kwargs: P.kwargs) -> T:
1227
+ # TODO write specific docs for this overload
1228
+ ...
1229
+
1230
+ @overload
1231
+ def __call__(
1232
+ self, *args: P.args, sync: Literal[False], **kwargs: P.kwargs
1233
+ ) -> Coroutine[Any, Any, T]:
1234
+ # TODO write specific docs for this overload
1235
+ ...
1236
+
1237
+ @overload
1238
+ def __call__(
1239
+ self, *args: P.args, asynchronous: Literal[False], **kwargs: P.kwargs
1240
+ ) -> T:
1241
+ # TODO write specific docs for this overload
1242
+ ...
1243
+
1244
+ @overload
1245
+ def __call__(
1246
+ self, *args: P.args, asynchronous: Literal[True], **kwargs: P.kwargs
1247
+ ) -> Coroutine[Any, Any, T]:
1248
+ # TODO write specific docs for this overload
1249
+ ...
1250
+
1251
+ @overload
1252
+ def __call__(self, *args: P.args, **kwargs: P.kwargs) -> Coroutine[Any, Any, T]: ...
1253
+ def __call__(_ASyncFunction self, *args: P.args, **kwargs: P.kwargs) -> MaybeCoro[T]:
1254
+ """Calls the wrapped function, defaulting to asynchronous execution.
1255
+
1256
+ This method overrides the base :meth:`ASyncFunction.__call__` to provide an asynchronous
1257
+ default behavior.
1258
+
1259
+ Args:
1260
+ *args: Positional arguments to pass to the wrapped function.
1261
+ **kwargs: Keyword arguments to pass to the wrapped function.
1262
+
1263
+ Raises:
1264
+ Exception: Any exception that may be raised by the wrapped function.
1265
+
1266
+ Returns:
1267
+ The result of the function call.
1268
+
1269
+ See Also:
1270
+ - :meth:`ASyncFunction.__call__`
1271
+ """
1272
+ return self.get_fn()(*args, **kwargs)
1273
+
1274
+ __docstring_append__ = ":class:`~a_sync.a_sync.function.ASyncFunctionAsyncDefault`, you can optionally pass `sync=True` or `asynchronous=False` to force it to run synchronously and return a value. Without either kwarg, it will return a coroutine for you to await."
1275
+
1276
+
1277
+ cdef class ASyncDecoratorSyncDefault(ASyncDecorator):
1278
+ @overload
1279
+ def __call__(self, func: AnyFn[Concatenate[B, P], T]) -> "ASyncBoundMethodSyncDefault[P, T]": # type: ignore [override]
1280
+ """
1281
+ Decorates a bound method with synchronous default behavior.
1282
+
1283
+ Args:
1284
+ func: The bound method to decorate.
1285
+
1286
+ Returns:
1287
+ An ASyncBoundMethodSyncDefault instance with synchronous default behavior.
1288
+
1289
+ See Also:
1290
+ - :class:`ASyncBoundMethodSyncDefault`
1291
+ """
1292
+
1293
+ @overload
1294
+ def __call__(self, func: AnyBoundMethod[P, T]) -> ASyncFunctionSyncDefault[P, T]: # type: ignore [override]
1295
+ """
1296
+ Decorates a bound method with synchronous default behavior.
1297
+
1298
+ Args:
1299
+ func: The bound method to decorate.
1300
+
1301
+ Returns:
1302
+ An ASyncFunctionSyncDefault instance with synchronous default behavior.
1303
+
1304
+ See Also:
1305
+ - :class:`ASyncFunctionSyncDefault`
1306
+ """
1307
+
1308
+ @overload
1309
+ def __call__(self, func: AnyFn[P, T]) -> ASyncFunctionSyncDefault[P, T]: # type: ignore [override]
1310
+ """
1311
+ Decorates a function with synchronous default behavior.
1312
+
1313
+ Args:
1314
+ func: The function to decorate.
1315
+
1316
+ Returns:
1317
+ An ASyncFunctionSyncDefault instance with synchronous default behavior.
1318
+
1319
+ See Also:
1320
+ - :class:`ASyncFunctionSyncDefault`
1321
+ """
1322
+
1323
+ def __call__(self, func: AnyFn[P, T]) -> ASyncFunctionSyncDefault[P, T]:
1324
+ # TODO write specific docs for this implementation
1325
+ return ASyncFunctionSyncDefault(func, **self.modifiers._modifiers)
1326
+
1327
+
1328
+ cdef class ASyncDecoratorAsyncDefault(ASyncDecorator):
1329
+ @overload
1330
+ def __call__(self, func: AnyFn[Concatenate[B, P], T]) -> "ASyncBoundMethodAsyncDefault[P, T]": # type: ignore [override]
1331
+ """
1332
+ Decorates a bound method with asynchronous default behavior.
1333
+
1334
+ Args:
1335
+ func: The bound method to decorate.
1336
+
1337
+ Returns:
1338
+ An ASyncBoundMethodAsyncDefault instance with asynchronous default behavior.
1339
+
1340
+ See Also:
1341
+ - :class:`ASyncBoundMethodAsyncDefault`
1342
+ """
1343
+
1344
+ @overload
1345
+ def __call__(self, func: AnyBoundMethod[P, T]) -> ASyncFunctionAsyncDefault[P, T]: # type: ignore [override]
1346
+ """
1347
+ Decorates a bound method with asynchronous default behavior.
1348
+
1349
+ Args:
1350
+ func: The bound method to decorate.
1351
+
1352
+ Returns:
1353
+ An ASyncFunctionAsyncDefault instance with asynchronous default behavior.
1354
+
1355
+ See Also:
1356
+ - :class:`ASyncFunctionAsyncDefault`
1357
+ """
1358
+
1359
+ @overload
1360
+ def __call__(self, func: AnyFn[P, T]) -> ASyncFunctionAsyncDefault[P, T]: # type: ignore [override]
1361
+ """
1362
+ Decorates a function with asynchronous default behavior.
1363
+
1364
+ Args:
1365
+ func: The function to decorate.
1366
+
1367
+ Returns:
1368
+ An ASyncFunctionAsyncDefault instance with asynchronous default behavior.
1369
+
1370
+ See Also:
1371
+ - :class:`ASyncFunctionAsyncDefault`
1372
+ """
1373
+
1374
+ def __call__(self, func: AnyFn[P, T]) -> ASyncFunctionAsyncDefault[P, T]:
1375
+ # TODO write specific docs for this implementation
1376
+ return ASyncFunctionAsyncDefault(func, **self.modifiers._modifiers)
1377
+
1378
+
1379
+ cdef inline void _import_TaskMapping():
1380
+ global TaskMapping
1381
+ from a_sync import TaskMapping