2323)
2424
2525from injection ._core .common .key import new_short_key
26- from injection ._core .slots import Slot
26+ from injection ._core .slots import SlotKey
2727from injection .exceptions import (
2828 InjectionError ,
2929 ScopeAlreadyDefinedError ,
@@ -127,6 +127,7 @@ def get_scope(self) -> Scope | None:
127127@asynccontextmanager
128128async def adefine_scope (
129129 name : str ,
130+ / ,
130131 kind : ScopeKind | ScopeKindStr = ScopeKind .get_default (),
131132) -> AsyncIterator [ScopeFacade ]:
132133 async with AsyncScope () as scope :
@@ -137,6 +138,7 @@ async def adefine_scope(
137138@contextmanager
138139def define_scope (
139140 name : str ,
141+ / ,
140142 kind : ScopeKind | ScopeKindStr = ScopeKind .get_default (),
141143) -> Iterator [ScopeFacade ]:
142144 with SyncScope () as scope :
@@ -208,7 +210,7 @@ def _bind_scope(
208210 )
209211
210212 with states [name ].bind (scope ):
211- yield ScopeFacade (scope )
213+ yield _UserScope (scope )
212214
213215
214216@runtime_checkable
@@ -286,19 +288,32 @@ def enter[T](self, context_manager: ContextManager[T]) -> T:
286288 return self .delegate .enter_context (context_manager )
287289
288290
291+ @runtime_checkable
292+ class ScopeFacade (Protocol ):
293+ __slots__ = ()
294+
295+ @abstractmethod
296+ def set_slot [T ](self , key : SlotKey [T ], value : T ) -> Self :
297+ raise NotImplementedError
298+
299+ @abstractmethod
300+ def slot_map (self , mapping : Mapping [SlotKey [Any ], Any ], / ) -> Self :
301+ raise NotImplementedError
302+
303+
289304@dataclass (repr = False , frozen = True , slots = True )
290- class ScopeFacade :
305+ class _UserScope ( ScopeFacade ) :
291306 scope : Scope
292307
293- def set_slot [T ](self , slot : Slot [T ], value : T ) -> Self :
294- return self .slot_map ({slot : value })
308+ def set_slot [T ](self , key : SlotKey [T ], value : T ) -> Self :
309+ return self .slot_map ({key : value })
295310
296- def slot_map (self , values : Mapping [Slot [Any ], Any ]) -> Self :
311+ def slot_map (self , mapping : Mapping [SlotKey [Any ], Any ], / ) -> Self :
297312 cache = self .scope .cache
298313
299- for slot in values :
300- if slot in cache :
314+ for slot_key in mapping :
315+ if slot_key in cache :
301316 raise InjectionError ("Slot already set." )
302317
303- cache .update (values )
318+ cache .update (mapping )
304319 return self
0 commit comments