|
1 | 1 | from abc import abstractmethod |
2 | | -from collections.abc import Awaitable, Callable |
3 | | -from contextlib import ContextDecorator |
| 2 | +from collections.abc import AsyncIterator, Awaitable, Callable, Iterator |
| 3 | +from contextlib import asynccontextmanager, contextmanager |
4 | 4 | from enum import Enum |
5 | 5 | from logging import Logger |
6 | | -from typing import ( |
7 | | - Any, |
8 | | - ContextManager, |
9 | | - Protocol, |
10 | | - Self, |
11 | | - final, |
12 | | - overload, |
13 | | - runtime_checkable, |
14 | | -) |
| 6 | +from typing import Any, Final, Protocol, Self, final, overload, runtime_checkable |
15 | 7 |
|
16 | 8 | from ._core.common.invertible import Invertible as _Invertible |
17 | 9 | from ._core.common.type import InputType as _InputType |
18 | 10 | from ._core.common.type import TypeInfo as _TypeInfo |
19 | 11 | from ._core.module import InjectableFactory as _InjectableFactory |
20 | 12 | from ._core.module import ModeStr, PriorityStr |
21 | 13 |
|
22 | | -__module: Module = ... |
| 14 | +__MODULE: Final[Module] = ... |
23 | 15 |
|
24 | | -afind_instance = __module.afind_instance |
25 | | -aget_instance = __module.aget_instance |
26 | | -aget_lazy_instance = __module.aget_lazy_instance |
27 | | -constant = __module.constant |
28 | | -find_instance = __module.find_instance |
29 | | -get_instance = __module.get_instance |
30 | | -get_lazy_instance = __module.get_lazy_instance |
31 | | -inject = __module.inject |
32 | | -injectable = __module.injectable |
33 | | -set_constant = __module.set_constant |
34 | | -should_be_injectable = __module.should_be_injectable |
35 | | -singleton = __module.singleton |
| 16 | +afind_instance = __MODULE.afind_instance |
| 17 | +aget_instance = __MODULE.aget_instance |
| 18 | +aget_lazy_instance = __MODULE.aget_lazy_instance |
| 19 | +constant = __MODULE.constant |
| 20 | +find_instance = __MODULE.find_instance |
| 21 | +get_instance = __MODULE.get_instance |
| 22 | +get_lazy_instance = __MODULE.get_lazy_instance |
| 23 | +inject = __MODULE.inject |
| 24 | +injectable = __MODULE.injectable |
| 25 | +scoped = __MODULE.scoped |
| 26 | +set_constant = __MODULE.set_constant |
| 27 | +should_be_injectable = __MODULE.should_be_injectable |
| 28 | +singleton = __MODULE.singleton |
36 | 29 |
|
| 30 | +@asynccontextmanager |
| 31 | +def adefine_scope(name: str, *, shared: bool = ...) -> AsyncIterator[None]: ... |
| 32 | +@contextmanager |
| 33 | +def define_scope(name: str, *, shared: bool = ...) -> Iterator[None]: ... |
37 | 34 | def mod(name: str = ..., /) -> Module: |
38 | 35 | """ |
39 | 36 | Short syntax for `Module.from_name`. |
@@ -109,6 +106,21 @@ class Module: |
109 | 106 | always be the same. |
110 | 107 | """ |
111 | 108 |
|
| 109 | + def scoped[**P, T]( |
| 110 | + self, |
| 111 | + scope_name: str, |
| 112 | + /, |
| 113 | + *, |
| 114 | + inject: bool = ..., |
| 115 | + on: _TypeInfo[T] = (), |
| 116 | + mode: Mode | ModeStr = ..., |
| 117 | + ) -> Any: |
| 118 | + """ |
| 119 | + Decorator applicable to a class or function or generator function. It is used |
| 120 | + to indicate how the scoped instance will be constructed. At injection time, the |
| 121 | + injected instance is retrieved from the scope. |
| 122 | + """ |
| 123 | + |
112 | 124 | def should_be_injectable[T](self, wrapped: type[T] = ..., /) -> Any: |
113 | 125 | """ |
114 | 126 | Decorator applicable to a class. It is used to specify whether an injectable |
@@ -248,12 +260,13 @@ class Module: |
248 | 260 | Function to remove a module in use. |
249 | 261 | """ |
250 | 262 |
|
| 263 | + @contextmanager |
251 | 264 | def use_temporarily( |
252 | 265 | self, |
253 | 266 | module: Module, |
254 | 267 | *, |
255 | 268 | priority: Priority | PriorityStr = ..., |
256 | | - ) -> ContextManager[None] | ContextDecorator: |
| 269 | + ) -> Iterator[None]: |
257 | 270 | """ |
258 | 271 | Context manager or decorator for temporary use of a module. |
259 | 272 | """ |
|
0 commit comments