Skip to content

Commit babfb3d

Browse files
authored
refactoring: ♻️ AsyncSemaphore
1 parent a7e4e23 commit babfb3d

5 files changed

Lines changed: 79 additions & 75 deletions

File tree

injection/__init__.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class Module:
149149
always be the same.
150150
"""
151151

152-
def scoped[**P, T](
152+
def scoped[T](
153153
self,
154154
scope_name: str,
155155
/,

injection/_core/common/asynchronous.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
from abc import abstractmethod
22
from collections.abc import Awaitable, Callable, Generator
33
from dataclasses import dataclass
4-
from typing import Any, AsyncContextManager, NoReturn, Protocol, runtime_checkable
4+
from typing import (
5+
Any,
6+
AsyncContextManager,
7+
NoReturn,
8+
Protocol,
9+
runtime_checkable,
10+
)
11+
12+
AsyncSemaphore: Callable[[int], AsyncContextManager[Any]]
13+
14+
try:
15+
import anyio
16+
17+
except ImportError: # pragma: no cover
18+
import asyncio
19+
20+
AsyncSemaphore = asyncio.Semaphore
21+
22+
else:
23+
AsyncSemaphore = anyio.Semaphore
524

625

726
@dataclass(repr=False, eq=False, frozen=True, slots=True)
@@ -45,16 +64,3 @@ async def acall(self, /, *args: P.args, **kwargs: P.kwargs) -> T:
4564

4665
def call(self, /, *args: P.args, **kwargs: P.kwargs) -> T:
4766
return self.callable(*args, **kwargs)
48-
49-
50-
try:
51-
import anyio
52-
53-
def create_semaphore(value: int) -> AsyncContextManager[Any]:
54-
return anyio.Semaphore(value)
55-
56-
except ImportError: # pragma: no cover
57-
import asyncio
58-
59-
def create_semaphore(value: int) -> AsyncContextManager[Any]:
60-
return asyncio.Semaphore(value)

injection/_core/injectables.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
runtime_checkable,
1414
)
1515

16-
from injection._core.common.asynchronous import Caller
17-
from injection._core.common.asynchronous import (
18-
create_semaphore as _create_async_semaphore,
19-
)
16+
from injection._core.common.asynchronous import AsyncSemaphore, Caller
2017
from injection._core.scope import (
2118
Scope,
2219
get_scope,
@@ -64,7 +61,7 @@ class CacheLogic[T]:
6461
__semaphore: AsyncContextManager[Any]
6562

6663
def __init__(self) -> None:
67-
self.__semaphore = _create_async_semaphore(1)
64+
self.__semaphore = AsyncSemaphore(1)
6865

6966
async def aget_or_create[K](
7067
self,

injection/ext/fastapi.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from dataclasses import dataclass, field
22
from types import GenericAlias
3-
from typing import Annotated, Any, TypeAliasType
3+
from typing import Annotated, Any, TypeAlias, TypeAliasType
44

55
from fastapi import Depends
66

@@ -25,16 +25,16 @@ def __call__[T](
2525
) -> Any:
2626
module = module or self.module
2727
threadsafe = self.threadsafe if threadsafe is None else threadsafe
28-
ainstance = module.aget_lazy_instance(cls, default, threadsafe=threadsafe)
28+
lazy_instance = module.aget_lazy_instance(cls, default, threadsafe=threadsafe)
2929

3030
async def dependency() -> T:
31-
return await ainstance
31+
return await lazy_instance
3232

3333
class_name = getattr(cls, "__name__", str(cls))
3434
dependency.__name__ = f"inject({class_name})"
3535
return Depends(dependency, use_cache=False)
3636

37-
def __getitem__(self, params: Any, /) -> Any:
37+
def __getitem__[T, *Ts](self, params: T | tuple[T, *Ts], /) -> TypeAlias:
3838
iter_params = iter(params if isinstance(params, tuple) else (params,))
3939
cls = next(iter_params)
4040
return Annotated[cls, self(cls), *iter_params]

0 commit comments

Comments
 (0)