@@ -83,16 +83,22 @@ def my_config(binder):
83
83
import logging
84
84
import sys
85
85
import threading
86
-
86
+ from typing import Optional , Type , Hashable , Callable , TypeVar , Union
87
87
88
88
logger = logging .getLogger ('inject' )
89
89
90
90
_INJECTOR = None # Shared injector instance.
91
91
_INJECTOR_LOCK = threading .RLock () # Guards injector initialization.
92
92
_BINDING_LOCK = threading .RLock () # Guards runtime bindings.
93
93
94
+ T = TypeVar ('T' )
95
+ Binding = Union [Type [T ], Hashable ]
96
+ Constructor = Provider = Callable [[], T ]
97
+ BinderCallable = Callable [['Binder' ], None ]
98
+
94
99
95
100
def configure (config = None , bind_in_runtime = True ):
101
+ # type: (Optional[BinderCallable], bool) -> Injector
96
102
"""Create an injector with a callable config or raise an exception when already configured."""
97
103
global _INJECTOR
98
104
@@ -106,6 +112,7 @@ def configure(config=None, bind_in_runtime=True):
106
112
107
113
108
114
def configure_once (config = None , bind_in_runtime = True ):
115
+ # type: (Optional[BinderCallable], bool) -> Injector
109
116
"""Create an injector with a callable config if not present, otherwise, do nothing."""
110
117
with _INJECTOR_LOCK :
111
118
if _INJECTOR :
@@ -115,19 +122,22 @@ def configure_once(config=None, bind_in_runtime=True):
115
122
116
123
117
124
def clear_and_configure (config = None , bind_in_runtime = True ):
125
+ # type: (Optional[BinderCallable], bool) -> Injector
118
126
"""Clear an existing injector and create another one with a callable config."""
119
127
with _INJECTOR_LOCK :
120
128
clear ()
121
129
return configure (config , bind_in_runtime = bind_in_runtime )
122
130
123
131
124
132
def is_configured ():
133
+ # type: () -> bool
125
134
"""Return true if an injector is already configured."""
126
135
with _INJECTOR_LOCK :
127
136
return _INJECTOR is not None
128
137
129
138
130
139
def clear ():
140
+ # type: () -> None
131
141
"""Clear an existing injector if present."""
132
142
global _INJECTOR
133
143
@@ -140,21 +150,25 @@ def clear():
140
150
141
151
142
152
def instance (cls ):
153
+ # type: (Binding) -> T
143
154
"""Inject an instance of a class."""
144
155
return get_injector_or_die ().get_instance (cls )
145
156
146
157
147
158
def attr (cls ):
159
+ # type: (Binding) -> T
148
160
"""Return a attribute injection (descriptor)."""
149
161
return _AttributeInjection (cls )
150
162
151
163
152
164
def param (name , cls = None ):
165
+ # type: (str, Binding) -> Callable
153
166
"""Deprecated, use @inject.params. Return a decorator which injects an arg into a function."""
154
167
return _ParameterInjection (name , cls )
155
168
156
169
157
170
def params (** args_to_classes ):
171
+ # type: (Binding) -> Callable
158
172
"""Return a decorator which injects args into a function.
159
173
160
174
For example::
@@ -167,6 +181,7 @@ def sign_up(name, email, cache, db):
167
181
168
182
169
183
def autoparams (* selected_args ):
184
+ # type: (str) -> Callable
170
185
"""Return a decorator that will inject args into a function using type annotations, Python >= 3.5 only.
171
186
172
187
For example::
@@ -201,11 +216,13 @@ def autoparams_decorator(func):
201
216
202
217
203
218
def get_injector ():
219
+ # type: () -> Injector
204
220
"""Return the current injector or None."""
205
221
return _INJECTOR
206
222
207
223
208
224
def get_injector_or_die ():
225
+ # type: () -> Injector
209
226
"""Return the current injector or raise an InjectorException."""
210
227
injector = _INJECTOR
211
228
if not injector :
@@ -219,18 +236,21 @@ def __init__(self):
219
236
self ._bindings = {}
220
237
221
238
def install (self , config ):
239
+ # type: (BinderCallable) -> Binder
222
240
"""Install another callable configuration."""
223
241
config (self )
224
242
return self
225
243
226
244
def bind (self , cls , instance ):
245
+ # type: (Binding, T) -> Binder
227
246
"""Bind a class to an instance."""
228
247
self ._check_class (cls )
229
248
self ._bindings [cls ] = lambda : instance
230
249
logger .debug ('Bound %s to an instance %s' , cls , instance )
231
250
return self
232
251
233
252
def bind_to_constructor (self , cls , constructor ):
253
+ # type: (Binding, Constructor) -> Binder
234
254
"""Bind a class to a callable singleton constructor."""
235
255
self ._check_class (cls )
236
256
if constructor is None :
@@ -241,6 +261,7 @@ def bind_to_constructor(self, cls, constructor):
241
261
return self
242
262
243
263
def bind_to_provider (self , cls , provider ):
264
+ # type: (Binding, Provider) -> Binder
244
265
"""Bind a class to a callable instance provider executed for each injection."""
245
266
self ._check_class (cls )
246
267
if provider is None :
@@ -251,6 +272,7 @@ def bind_to_provider(self, cls, provider):
251
272
return self
252
273
253
274
def _check_class (self , cls ):
275
+ # type: (Binding) -> None
254
276
if cls is None :
255
277
raise InjectorException ('Binding key cannot be None' )
256
278
@@ -269,6 +291,7 @@ def __init__(self, config=None, bind_in_runtime=True):
269
291
self ._bindings = {}
270
292
271
293
def get_instance (self , cls ):
294
+ # type: (Binding) -> T
272
295
"""Return an instance for a class."""
273
296
binding = self ._bindings .get (cls )
274
297
if binding :
0 commit comments