1
1
from abc import abstractmethod
2
- from collections .abc import Callable
2
+ from collections .abc import Awaitable , Callable
3
3
from contextlib import ContextDecorator
4
4
from enum import Enum
5
5
from logging import Logger
@@ -21,6 +21,9 @@ from ._core.module import ModeStr, PriorityStr
21
21
22
22
_ : Module = ...
23
23
24
+ afind_instance = _ .afind_instance
25
+ aget_instance = _ .aget_instance
26
+ aget_lazy_instance = _ .aget_lazy_instance
24
27
constant = _ .constant
25
28
find_instance = _ .find_instance
26
29
get_instance = _ .get_instance
@@ -53,59 +56,59 @@ class Module:
53
56
def __contains__ (self , cls : _InputType [Any ], / ) -> bool : ...
54
57
@property
55
58
def is_locked (self ) -> bool : ...
56
- def inject [** P , T ](self , wrapped : Callable [P , T ] = ..., / ): # type: ignore[no-untyped-def]
59
+ def inject [** P , T ](self , wrapped : Callable [P , T ] = ..., / ) -> Any :
57
60
"""
58
61
Decorator applicable to a class or function. Inject function dependencies using
59
62
parameter type annotations. If applied to a class, the dependencies resolved
60
63
will be those of the `__init__` method.
61
64
"""
62
65
63
- def injectable [** P , T ]( # type: ignore[no-untyped-def]
66
+ def injectable [** P , T ](
64
67
self ,
65
- wrapped : Callable [P , T ] = ...,
68
+ wrapped : Callable [P , T ] | Callable [ P , Awaitable [ T ]] = ...,
66
69
/ ,
67
70
* ,
68
71
cls : _InjectableFactory [T ] = ...,
69
72
inject : bool = ...,
70
73
on : _TypeInfo [T ] = ...,
71
74
mode : Mode | ModeStr = ...,
72
- ):
75
+ ) -> Any :
73
76
"""
74
77
Decorator applicable to a class or function. It is used to indicate how the
75
78
injectable will be constructed. At injection time, a new instance will be
76
79
injected each time.
77
80
"""
78
81
79
- def singleton [** P , T ]( # type: ignore[no-untyped-def]
82
+ def singleton [** P , T ](
80
83
self ,
81
- wrapped : Callable [P , T ] = ...,
84
+ wrapped : Callable [P , T ] | Callable [ P , Awaitable [ T ]] = ...,
82
85
/ ,
83
86
* ,
84
87
inject : bool = ...,
85
88
on : _TypeInfo [T ] = ...,
86
89
mode : Mode | ModeStr = ...,
87
- ):
90
+ ) -> Any :
88
91
"""
89
92
Decorator applicable to a class or function. It is used to indicate how the
90
93
singleton will be constructed. At injection time, the injected instance will
91
94
always be the same.
92
95
"""
93
96
94
- def should_be_injectable [T ](self , wrapped : type [T ] = ..., / ): # type: ignore[no-untyped-def]
97
+ def should_be_injectable [T ](self , wrapped : type [T ] = ..., / ) -> Any :
95
98
"""
96
99
Decorator applicable to a class. It is used to specify whether an injectable
97
100
should be registered. Raise an exception at injection time if the class isn't
98
101
registered.
99
102
"""
100
103
101
- def constant [T ]( # type: ignore[no-untyped-def]
104
+ def constant [T ](
102
105
self ,
103
106
wrapped : type [T ] = ...,
104
107
/ ,
105
108
* ,
106
109
on : _TypeInfo [T ] = ...,
107
110
mode : Mode | ModeStr = ...,
108
- ):
111
+ ) -> Any :
109
112
"""
110
113
Decorator applicable to a class or function. It is used to indicate how the
111
114
constant is constructed. At injection time, the injected instance will always
@@ -131,12 +134,25 @@ class Module:
131
134
wrapped : Callable [P , T ],
132
135
/ ,
133
136
) -> Callable [P , T ]: ...
137
+ async def afind_instance [T ](self , cls : _InputType [T ]) -> T : ...
134
138
def find_instance [T ](self , cls : _InputType [T ]) -> T :
135
139
"""
136
140
Function used to retrieve an instance associated with the type passed in
137
141
parameter or an exception will be raised.
138
142
"""
139
143
144
+ @overload
145
+ async def aget_instance [T , Default ](
146
+ self ,
147
+ cls : _InputType [T ],
148
+ default : Default ,
149
+ ) -> T | Default : ...
150
+ @overload
151
+ async def aget_instance [T ](
152
+ self ,
153
+ cls : _InputType [T ],
154
+ default : None = ...,
155
+ ) -> T | None : ...
140
156
@overload
141
157
def get_instance [T , Default ](
142
158
self ,
@@ -149,12 +165,28 @@ class Module:
149
165
"""
150
166
151
167
@overload
152
- def get_instance [T , _ ](
168
+ def get_instance [T ](
153
169
self ,
154
170
cls : _InputType [T ],
155
171
default : None = ...,
156
172
) -> T | None : ...
157
173
@overload
174
+ def aget_lazy_instance [T , Default ](
175
+ self ,
176
+ cls : _InputType [T ],
177
+ default : Default ,
178
+ * ,
179
+ cache : bool = ...,
180
+ ) -> Awaitable [T | Default ]: ...
181
+ @overload
182
+ def aget_lazy_instance [T ](
183
+ self ,
184
+ cls : _InputType [T ],
185
+ default : None = ...,
186
+ * ,
187
+ cache : bool = ...,
188
+ ) -> Awaitable [T | None ]: ...
189
+ @overload
158
190
def get_lazy_instance [T , Default ](
159
191
self ,
160
192
cls : _InputType [T ],
@@ -172,7 +204,7 @@ class Module:
172
204
"""
173
205
174
206
@overload
175
- def get_lazy_instance [T , _ ](
207
+ def get_lazy_instance [T ](
176
208
self ,
177
209
cls : _InputType [T ],
178
210
default : None = ...,
@@ -229,6 +261,7 @@ class Module:
229
261
Function to unlock the module by deleting cached instances of singletons.
230
262
"""
231
263
264
+ async def all_ready (self ) -> None : ...
232
265
def add_logger (self , logger : Logger ) -> Self : ...
233
266
@classmethod
234
267
def from_name (cls , name : str ) -> Module :
@@ -253,6 +286,8 @@ class Injectable[T](Protocol):
253
286
def is_locked (self ) -> bool : ...
254
287
def unlock (self ) -> None : ...
255
288
@abstractmethod
289
+ async def aget_instance (self ) -> T : ...
290
+ @abstractmethod
256
291
def get_instance (self ) -> T : ...
257
292
258
293
@final
0 commit comments