auto-workflow 0.1.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.
auto_workflow/utils.py ADDED
@@ -0,0 +1,39 @@
1
+ """Utility helpers."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import asyncio
6
+ import functools
7
+ import hashlib
8
+ import inspect
9
+ from collections.abc import Callable
10
+ from typing import Any
11
+
12
+ _DEF_HASH_SALT = b"auto_workflow:v1"
13
+
14
+
15
+ def default_cache_key(fn: Callable[..., Any], args: tuple[Any, ...], kwargs: dict[str, Any]) -> str:
16
+ try:
17
+ sig = inspect.signature(fn)
18
+ bound = sig.bind_partial(*args, **kwargs)
19
+ bound.apply_defaults()
20
+ key_source = repr((fn.__module__, fn.__qualname__, sorted(bound.arguments.items())))
21
+ except Exception: # pragma: no cover - fallback
22
+ key_source = repr((fn.__module__, fn.__qualname__, args, kwargs))
23
+ digest = hashlib.sha256(_DEF_HASH_SALT + key_source.encode()).hexdigest()
24
+ return digest
25
+
26
+
27
+ def is_coroutine_fn(fn: Callable[..., Any]) -> bool:
28
+ while isinstance(fn, functools.partial): # type: ignore
29
+ fn = fn.func # type: ignore
30
+ return asyncio.iscoroutinefunction(fn) or inspect.isasyncgenfunction(fn)
31
+
32
+
33
+ async def maybe_await(value: Any) -> Any:
34
+ if inspect.isawaitable(value):
35
+ return await value
36
+ return value
37
+
38
+
39
+ __all__ = ["default_cache_key", "is_coroutine_fn", "maybe_await"]