Skip to content

Commit 6103193

Browse files
author
remimd
committed
refactor: Update ScopedInjectable to use SlotKey for caching logic
1 parent bfc630c commit 6103193

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

injection/_core/injectables.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,12 @@ def unlock(self) -> None:
122122
class ScopedInjectable[R, T](Injectable[T], ABC):
123123
factory: Caller[..., R]
124124
scope_name: str
125+
key: SlotKey[T] = field(default_factory=SlotKey)
125126
logic: CacheLogic[T] = field(default_factory=CacheLogic)
126127

127128
@property
128129
def is_locked(self) -> bool:
129-
return in_scope_cache(self, self.scope_name)
130+
return in_scope_cache(self.key, self.scope_name)
130131

131132
@abstractmethod
132133
async def abuild(self, scope: Scope) -> T:
@@ -139,12 +140,12 @@ def build(self, scope: Scope) -> T:
139140
async def aget_instance(self) -> T:
140141
scope = self.__get_scope()
141142
factory = partial(self.abuild, scope)
142-
return await self.logic.aget_or_create(scope.cache, self, factory)
143+
return await self.logic.aget_or_create(scope.cache, self.key, factory)
143144

144145
def get_instance(self) -> T:
145146
scope = self.__get_scope()
146147
factory = partial(self.build, scope)
147-
return self.logic.get_or_create(scope.cache, self, factory)
148+
return self.logic.get_or_create(scope.cache, self.key, factory)
148149

149150
def unlock(self) -> None:
150151
if self.is_locked:
@@ -187,7 +188,7 @@ def build(self, scope: Scope) -> T:
187188
return self.factory.call()
188189

189190
def unlock(self) -> None:
190-
remove_scoped_values(self, self.scope_name)
191+
remove_scoped_values(self.key, self.scope_name)
191192

192193

193194
@dataclass(repr=False, eq=False, frozen=True, slots=True)

injection/_core/scope.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,11 @@ def get_scope[T](name: str, default: T | EllipsisType = ...) -> Scope | T:
182182
return default
183183

184184

185-
def in_scope_cache(key: Any, scope_name: str) -> bool:
185+
def in_scope_cache(key: SlotKey[Any], scope_name: str) -> bool:
186186
return any(key in scope.cache for scope in get_active_scopes(scope_name))
187187

188188

189-
def remove_scoped_values(key: Any, scope_name: str) -> None:
189+
def remove_scoped_values(key: SlotKey[Any], scope_name: str) -> None:
190190
for scope in get_active_scopes(scope_name):
191191
scope.cache.pop(key, None)
192192

@@ -233,7 +233,7 @@ def _bind_scope(
233233
class Scope(Protocol):
234234
__slots__ = ()
235235

236-
cache: MutableMapping[Any, Any]
236+
cache: MutableMapping[SlotKey[Any], Any]
237237

238238
@abstractmethod
239239
async def aenter[T](self, context_manager: AsyncContextManager[T]) -> T:
@@ -247,7 +247,7 @@ def enter[T](self, context_manager: ContextManager[T]) -> T:
247247
@dataclass(repr=False, frozen=True, slots=True)
248248
class BaseScope[T](Scope, ABC):
249249
delegate: T
250-
cache: MutableMapping[Any, Any] = field(
250+
cache: MutableMapping[SlotKey[Any], Any] = field(
251251
default_factory=dict,
252252
init=False,
253253
hash=False,

uv.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)