haiway 0.19.4__py3-none-any.whl → 0.20.0__py3-none-any.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.
@@ -43,28 +43,55 @@ def throttle[**Args, Result](
43
43
  ]
44
44
  | Callable[Args, Coroutine[Any, Any, Result]]
45
45
  ):
46
- """\
47
- Throttle for function calls with custom limit and period time. \
48
- Works only for async functions by waiting desired time before execution. \
49
- It is not allowed to be used on class or instance methods. \
50
- This wrapper is not thread safe.
46
+ """
47
+ Rate-limit asynchronous function calls.
48
+
49
+ This decorator restricts the frequency of function calls by enforcing a maximum
50
+ number of executions within a specified time period. When the limit is reached,
51
+ subsequent calls will wait until they can be executed without exceeding the limit.
52
+
53
+ Can be used as a simple decorator (@throttle) or with configuration
54
+ parameters (@throttle(limit=5, period=60)).
51
55
 
52
56
  Parameters
53
57
  ----------
54
- function: Callable[Args, Coroutine[Any, Any, Result]]
55
- function to wrap in throttle
58
+ function: Callable[Args, Coroutine[Any, Any, Result]] | None
59
+ The async function to throttle. When used as a simple decorator,
60
+ this parameter is provided automatically.
56
61
  limit: int
57
- limit of executions in given period, if no period was specified
58
- it is number of concurrent executions instead, default is 1
59
- period: timedelta | float | None
60
- period time (in seconds by default) during which the limit resets, default is 1 second
62
+ Maximum number of executions allowed within the specified period.
63
+ Default is 1, meaning only one call is allowed per period.
64
+ period: timedelta | float
65
+ Time window in which the limit applies. Can be specified as a timedelta
66
+ object or as a float (seconds). Default is 1 second.
61
67
 
62
68
  Returns
63
69
  -------
64
- Callable[[Callable[Args, Coroutine[Any, Any, Result]]], Callable[Args, Coroutine[Any, Any, Result]]] \
65
- | Callable[Args, Coroutine[Any, Any, Result]]
66
- provided function wrapped in throttle
67
- """ # noqa: E501
70
+ Callable
71
+ When used as @throttle: Returns the wrapped function that enforces the rate limit.
72
+ When used as @throttle(...): Returns a decorator that can be applied to a function.
73
+
74
+ Notes
75
+ -----
76
+ - Works only with asynchronous functions.
77
+ - Cannot be used on class or instance methods.
78
+ - Not thread-safe, should only be used within a single event loop.
79
+ - The function preserves the original function's signature, docstring, and other attributes.
80
+
81
+ Examples
82
+ --------
83
+ Basic usage to limit to 1 call per second:
84
+
85
+ >>> @throttle
86
+ ... async def api_call(data):
87
+ ... return await external_api.send(data)
88
+
89
+ Limit to 5 calls per minute:
90
+
91
+ >>> @throttle(limit=5, period=60)
92
+ ... async def api_call(data):
93
+ ... return await external_api.send(data)
94
+ """
68
95
 
69
96
  def _wrap(
70
97
  function: Callable[Args, Coroutine[Any, Any, Result]],
@@ -14,23 +14,38 @@ def timeout[**Args, Result](
14
14
  [Callable[Args, Coroutine[Any, Any, Result]]],
15
15
  Callable[Args, Coroutine[Any, Any, Result]],
16
16
  ]:
17
- """\
18
- Timeout wrapper for a function call. \
19
- When the timeout time will pass before function returns function execution will be \
20
- cancelled and TimeoutError exception will raise. Make sure that wrapped \
21
- function handles cancellation properly.
22
- This wrapper is not thread safe.
17
+ """
18
+ Add a timeout to an asynchronous function.
19
+
20
+ This decorator enforces a maximum execution time for the decorated function.
21
+ If the function does not complete within the specified timeout period, it
22
+ will be cancelled and a TimeoutError will be raised.
23
23
 
24
24
  Parameters
25
25
  ----------
26
26
  timeout: float
27
- timeout time in seconds
27
+ Maximum execution time in seconds allowed for the function
28
28
 
29
29
  Returns
30
30
  -------
31
- Callable[[Callable[_Args, _Result]], Callable[_Args, _Result]] | Callable[_Args, _Result]
32
- function wrapper adding timeout
33
- """
31
+ Callable[[Callable[Args, Coroutine[Any, Any, Result]]], Callable[Args, Coroutine[Any, Any, Result]]]
32
+ A decorator that can be applied to an async function to add timeout behavior
33
+
34
+ Notes
35
+ -----
36
+ - Works only with asynchronous functions.
37
+ - The wrapped function will be properly cancelled when the timeout occurs.
38
+ - Not thread-safe, should only be used within a single event loop.
39
+ - The original function should handle cancellation properly to ensure
40
+ resources are released when timeout occurs.
41
+
42
+ Examples
43
+ --------
44
+ >>> @timeout(5.0)
45
+ ... async def fetch_data(url):
46
+ ... # Will raise TimeoutError if it takes more than 5 seconds
47
+ ... return await http_client.get(url)
48
+ """ # noqa: E501
34
49
 
35
50
  def _wrap(
36
51
  function: Callable[Args, Coroutine[Any, Any, Result]],
haiway/helpers/tracing.py CHANGED
@@ -33,6 +33,37 @@ def traced[**Args, Result](
33
33
  level: ObservabilityLevel = ObservabilityLevel.DEBUG,
34
34
  label: str | None = None,
35
35
  ) -> Callable[[Callable[Args, Result]], Callable[Args, Result]] | Callable[Args, Result]:
36
+ """
37
+ Decorator that adds tracing to functions, recording inputs, outputs, and exceptions.
38
+
39
+ Automatically records function arguments, return values, and any exceptions
40
+ within the current observability context. The recorded data can be used for
41
+ debugging, performance analysis, and understanding program execution flow.
42
+
43
+ In non-debug builds (when __debug__ is False), this decorator has no effect
44
+ and returns the original function to avoid performance impact in production.
45
+
46
+ Parameters
47
+ ----------
48
+ function: Callable[Args, Result] | None
49
+ The function to be traced
50
+ level: ObservabilityLevel
51
+ The observability level at which to record trace information (default: DEBUG)
52
+ label: str | None
53
+ Custom label for the trace; defaults to the function name if not provided
54
+
55
+ Returns
56
+ -------
57
+ Callable
58
+ A decorated function that performs the same operation as the original
59
+ but with added tracing
60
+
61
+ Notes
62
+ -----
63
+ Works with both synchronous and asynchronous functions. For asynchronous
64
+ functions, properly awaits the result before recording it.
65
+ """
66
+
36
67
  def wrap(
37
68
  wrapped: Callable[Args, Result],
38
69
  ) -> Callable[Args, Result]: