anydi 0.41.0__py3-none-any.whl → 0.43.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.
anydi/__init__.py CHANGED
@@ -1,20 +1,14 @@
1
1
  """AnyDI public objects and functions."""
2
2
 
3
- from typing import Any, cast
4
-
5
- from ._container import (
6
- Container,
7
- Module,
8
- injectable,
9
- provider,
10
- request,
11
- singleton,
12
- transient,
13
- )
14
- from ._types import Marker, ProviderArgs as Provider, Scope
3
+ from ._container import Container
4
+ from ._decorators import injectable, provided, provider, request, singleton, transient
5
+ from ._module import Module
6
+ from ._provider import ProviderDef as Provider
7
+ from ._scope import Scope
8
+ from ._typing import Marker
15
9
 
16
10
  # Alias for dependency auto marker
17
- auto = cast(Any, Marker())
11
+ auto = Marker()
18
12
 
19
13
 
20
14
  __all__ = [
@@ -28,4 +22,5 @@ __all__ = [
28
22
  "request",
29
23
  "singleton",
30
24
  "transient",
25
+ "provided",
31
26
  ]
anydi/_async.py ADDED
@@ -0,0 +1,50 @@
1
+ import functools
2
+ from types import TracebackType
3
+ from typing import Any, Callable, TypeVar
4
+
5
+ import anyio.to_thread
6
+ from typing_extensions import ParamSpec, Self
7
+
8
+ T = TypeVar("T")
9
+ P = ParamSpec("P")
10
+
11
+
12
+ async def run_sync(func: Callable[P, T], /, *args: P.args, **kwargs: P.kwargs) -> T:
13
+ """Runs the given function asynchronously using the `anyio` library."""
14
+ return await anyio.to_thread.run_sync(functools.partial(func, *args, **kwargs))
15
+
16
+
17
+ class AsyncRLock:
18
+ def __init__(self) -> None:
19
+ self._lock = anyio.Lock()
20
+ self._owner: anyio.TaskInfo | None = None
21
+ self._count = 0
22
+
23
+ async def acquire(self) -> None:
24
+ current_task = anyio.get_current_task()
25
+ if self._owner == current_task:
26
+ self._count += 1
27
+ else:
28
+ await self._lock.acquire()
29
+ self._owner = current_task
30
+ self._count = 1
31
+
32
+ def release(self) -> None:
33
+ if self._owner != anyio.get_current_task():
34
+ raise RuntimeError("Lock can only be released by the owner")
35
+ self._count -= 1
36
+ if self._count == 0:
37
+ self._owner = None
38
+ self._lock.release()
39
+
40
+ async def __aenter__(self) -> Self:
41
+ await self.acquire()
42
+ return self
43
+
44
+ async def __aexit__(
45
+ self,
46
+ exc_type: type[BaseException] | None,
47
+ exc_val: BaseException | None,
48
+ exc_tb: TracebackType | None,
49
+ ) -> Any:
50
+ self.release()