Selector provider (#258)

* Add Selector provider

* Add Selector provider example

* Add tests for selector provider

* Update changelog

* Add pydocstyle ignore D101 for examples

* Fix repr() test

* Update changelog

* Add D203,D213 to examples pydocstyle ignore

* Add selector provider docs
This commit is contained in:
Roman Mogylatov 2020-06-29 16:32:12 -04:00 committed by GitHub
parent 783be3ad36
commit 2513d1f600
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 5182 additions and 787 deletions

View File

@ -7,6 +7,11 @@ that were made in every particular version.
From version 0.7.6 *Dependency Injector* framework strictly
follows `Semantic versioning`_
Development version
-------------------
- Add ``Selector`` provider.
- Fix ``Configuration.override()`` to return ``OverridingContext`` for non-dictionary values.
3.18.1
------
- Add interpolation of environment variables to ``Configuration.from_yaml()`` and

View File

@ -24,6 +24,7 @@ Providers package API docs - :py:mod:`dependency_injector.providers`
object
list
configuration
selector
dependency
overriding
custom

View File

@ -0,0 +1,31 @@
Selector providers
------------------
.. currentmodule:: dependency_injector.providers
:py:class:`Selector` provider selects provider based on the configuration value or other callable.
.. literalinclude:: ../../examples/providers/selector.py
:language: python
:emphasize-lines: 6-10
:lines: 3-5,14-20
:linenos:
:py:class:`Selector` provider has a callable called ``selector`` and a dictionary of providers.
The ``selector`` callable is provided as a first positional argument. It can be
:py:class:`Configuration` provider or any other callable. It has to return a string value.
That value is used as a key for selecting the provider from the dictionary of providers.
The providers are provided as keyword arguments. Argument name is used as a key for
selecting the provider.
Full example:
.. literalinclude:: ../../examples/providers/selector.py
:language: python
:emphasize-lines: 14-18
:lines: 3-
:linenos:
.. disqus::

2
examples/.pydocstylerc Normal file
View File

@ -0,0 +1,2 @@
[pydocstyle]
ignore = D101,D203,D213

View File

@ -0,0 +1,28 @@
"""`Selector` provider example."""
from dependency_injector import providers
class SomeClass:
...
class SomeOtherClass:
...
config = providers.Configuration()
selector = providers.Selector(
config.one_or_another,
one=providers.Factory(SomeClass),
another=providers.Factory(SomeOtherClass),
)
config.override({'one_or_another': 'one'})
instance_1 = selector()
assert isinstance(instance_1, SomeClass)
config.override({'one_or_another': 'another'})
instance_2 = selector()
assert isinstance(instance_2, SomeOtherClass)

View File

@ -846,6 +846,7 @@ struct __pyx_obj_19dependency_injector_9providers_AbstractSingleton;
struct __pyx_obj_19dependency_injector_9providers_SingletonDelegate;
struct __pyx_obj_19dependency_injector_9providers_List;
struct __pyx_obj_19dependency_injector_9providers_Container;
struct __pyx_obj_19dependency_injector_9providers_Selector;
struct __pyx_obj_19dependency_injector_9providers_Injection;
struct __pyx_obj_19dependency_injector_9providers_PositionalInjection;
struct __pyx_obj_19dependency_injector_9providers_NamedInjection;
@ -857,7 +858,7 @@ struct __pyx_obj_19dependency_injector_10containers___pyx_scope_struct_4_overrid
struct __pyx_obj_19dependency_injector_10containers___pyx_scope_struct_5_copy;
struct __pyx_opt_args_19dependency_injector_9providers_deepcopy;
/* "providers.pxd":225
/* "providers.pxd":232
*
*
* cpdef object deepcopy(object instance, dict memo=*) # <<<<<<<<<<<<<<
@ -1284,7 +1285,21 @@ struct __pyx_obj_19dependency_injector_9providers_Container {
};
/* "providers.pxd":188
/* "providers.pxd":187
*
*
* cdef class Selector(Provider): # <<<<<<<<<<<<<<
* cdef object __selector
* cdef dict __providers
*/
struct __pyx_obj_19dependency_injector_9providers_Selector {
struct __pyx_obj_19dependency_injector_9providers_Provider __pyx_base;
PyObject *__pyx___selector;
PyObject *__pyx___providers;
};
/* "providers.pxd":195
*
* # Injections
* cdef class Injection(object): # <<<<<<<<<<<<<<
@ -1300,7 +1315,7 @@ struct __pyx_obj_19dependency_injector_9providers_Injection {
};
/* "providers.pxd":195
/* "providers.pxd":202
*
*
* cdef class PositionalInjection(Injection): # <<<<<<<<<<<<<<
@ -1312,7 +1327,7 @@ struct __pyx_obj_19dependency_injector_9providers_PositionalInjection {
};
/* "providers.pxd":199
/* "providers.pxd":206
*
*
* cdef class NamedInjection(Injection): # <<<<<<<<<<<<<<
@ -1859,6 +1874,20 @@ struct __pyx_vtabstruct_19dependency_injector_9providers_Container {
};
static struct __pyx_vtabstruct_19dependency_injector_9providers_Container *__pyx_vtabptr_19dependency_injector_9providers_Container;
/* "providers.pxd":187
*
*
* cdef class Selector(Provider): # <<<<<<<<<<<<<<
* cdef object __selector
* cdef dict __providers
*/
struct __pyx_vtabstruct_19dependency_injector_9providers_Selector {
struct __pyx_vtabstruct_19dependency_injector_9providers_Provider __pyx_base;
};
static struct __pyx_vtabstruct_19dependency_injector_9providers_Selector *__pyx_vtabptr_19dependency_injector_9providers_Selector;
/* --- Runtime support code (head) --- */
/* Refnanny.proto */
#ifndef CYTHON_REFNANNY
@ -2507,6 +2536,7 @@ static PyTypeObject *__pyx_ptype_19dependency_injector_9providers_AbstractSingle
static PyTypeObject *__pyx_ptype_19dependency_injector_9providers_SingletonDelegate = 0;
static PyTypeObject *__pyx_ptype_19dependency_injector_9providers_List = 0;
static PyTypeObject *__pyx_ptype_19dependency_injector_9providers_Container = 0;
static PyTypeObject *__pyx_ptype_19dependency_injector_9providers_Selector = 0;
static PyTypeObject *__pyx_ptype_19dependency_injector_9providers_Injection = 0;
static PyTypeObject *__pyx_ptype_19dependency_injector_9providers_PositionalInjection = 0;
static PyTypeObject *__pyx_ptype_19dependency_injector_9providers_NamedInjection = 0;
@ -9302,7 +9332,7 @@ static PyObject *__pyx_pf_19dependency_injector_10containers_6_check_provider_ty
return __pyx_r;
}
/* "providers.pxd":229
/* "providers.pxd":236
*
* # Inline helper functions
* cdef inline object __get_name(NamedInjection self): # <<<<<<<<<<<<<<
@ -9315,7 +9345,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___get_na
__Pyx_RefNannyDeclarations
__Pyx_RefNannySetupContext("__get_name", 0);
/* "providers.pxd":230
/* "providers.pxd":237
* # Inline helper functions
* cdef inline object __get_name(NamedInjection self):
* return self.__name # <<<<<<<<<<<<<<
@ -9327,7 +9357,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___get_na
__pyx_r = __pyx_v_self->__pyx___name;
goto __pyx_L0;
/* "providers.pxd":229
/* "providers.pxd":236
*
* # Inline helper functions
* cdef inline object __get_name(NamedInjection self): # <<<<<<<<<<<<<<
@ -9342,7 +9372,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___get_na
return __pyx_r;
}
/* "providers.pxd":233
/* "providers.pxd":240
*
*
* cdef inline object __get_value(Injection self): # <<<<<<<<<<<<<<
@ -9362,7 +9392,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___get_va
int __pyx_clineno = 0;
__Pyx_RefNannySetupContext("__get_value", 0);
/* "providers.pxd":234
/* "providers.pxd":241
*
* cdef inline object __get_value(Injection self):
* if self.__call == 0: # <<<<<<<<<<<<<<
@ -9372,7 +9402,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___get_va
__pyx_t_1 = ((__pyx_v_self->__pyx___call == 0) != 0);
if (__pyx_t_1) {
/* "providers.pxd":235
/* "providers.pxd":242
* cdef inline object __get_value(Injection self):
* if self.__call == 0:
* return self.__value # <<<<<<<<<<<<<<
@ -9384,7 +9414,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___get_va
__pyx_r = __pyx_v_self->__pyx___value;
goto __pyx_L0;
/* "providers.pxd":234
/* "providers.pxd":241
*
* cdef inline object __get_value(Injection self):
* if self.__call == 0: # <<<<<<<<<<<<<<
@ -9393,7 +9423,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___get_va
*/
}
/* "providers.pxd":236
/* "providers.pxd":243
* if self.__call == 0:
* return self.__value
* return self.__value() # <<<<<<<<<<<<<<
@ -9414,14 +9444,14 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___get_va
}
__pyx_t_2 = (__pyx_t_4) ? __Pyx_PyObject_CallOneArg(__pyx_t_3, __pyx_t_4) : __Pyx_PyObject_CallNoArg(__pyx_t_3);
__Pyx_XDECREF(__pyx_t_4); __pyx_t_4 = 0;
if (unlikely(!__pyx_t_2)) __PYX_ERR(1, 236, __pyx_L1_error)
if (unlikely(!__pyx_t_2)) __PYX_ERR(1, 243, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_2);
__Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
__pyx_r = __pyx_t_2;
__pyx_t_2 = 0;
goto __pyx_L0;
/* "providers.pxd":233
/* "providers.pxd":240
*
*
* cdef inline object __get_value(Injection self): # <<<<<<<<<<<<<<
@ -9442,7 +9472,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___get_va
return __pyx_r;
}
/* "providers.pxd":241
/* "providers.pxd":248
* @cython.boundscheck(False)
* @cython.wraparound(False)
* cdef inline tuple __provide_positional_args(tuple args, # <<<<<<<<<<<<<<
@ -9467,7 +9497,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___provid
int __pyx_clineno = 0;
__Pyx_RefNannySetupContext("__provide_positional_args", 0);
/* "providers.pxd":248
/* "providers.pxd":255
* cdef PositionalInjection injection
*
* if inj_args_len == 0: # <<<<<<<<<<<<<<
@ -9477,7 +9507,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___provid
__pyx_t_1 = ((__pyx_v_inj_args_len == 0) != 0);
if (__pyx_t_1) {
/* "providers.pxd":249
/* "providers.pxd":256
*
* if inj_args_len == 0:
* return args # <<<<<<<<<<<<<<
@ -9489,7 +9519,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___provid
__pyx_r = __pyx_v_args;
goto __pyx_L0;
/* "providers.pxd":248
/* "providers.pxd":255
* cdef PositionalInjection injection
*
* if inj_args_len == 0: # <<<<<<<<<<<<<<
@ -9498,19 +9528,19 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___provid
*/
}
/* "providers.pxd":251
/* "providers.pxd":258
* return args
*
* positional_args = list() # <<<<<<<<<<<<<<
* for index in range(inj_args_len):
* injection = <PositionalInjection>inj_args[index]
*/
__pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) __PYX_ERR(1, 251, __pyx_L1_error)
__pyx_t_2 = PyList_New(0); if (unlikely(!__pyx_t_2)) __PYX_ERR(1, 258, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_2);
__pyx_v_positional_args = ((PyObject*)__pyx_t_2);
__pyx_t_2 = 0;
/* "providers.pxd":252
/* "providers.pxd":259
*
* positional_args = list()
* for index in range(inj_args_len): # <<<<<<<<<<<<<<
@ -9522,7 +9552,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___provid
for (__pyx_t_5 = 0; __pyx_t_5 < __pyx_t_4; __pyx_t_5+=1) {
__pyx_v_index = __pyx_t_5;
/* "providers.pxd":253
/* "providers.pxd":260
* positional_args = list()
* for index in range(inj_args_len):
* injection = <PositionalInjection>inj_args[index] # <<<<<<<<<<<<<<
@ -9531,36 +9561,36 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___provid
*/
if (unlikely(__pyx_v_inj_args == Py_None)) {
PyErr_SetString(PyExc_TypeError, "'NoneType' object is not subscriptable");
__PYX_ERR(1, 253, __pyx_L1_error)
__PYX_ERR(1, 260, __pyx_L1_error)
}
__pyx_t_2 = PyTuple_GET_ITEM(__pyx_v_inj_args, __pyx_v_index);
__Pyx_INCREF(__pyx_t_2);
__Pyx_XDECREF_SET(__pyx_v_injection, ((struct __pyx_obj_19dependency_injector_9providers_PositionalInjection *)__pyx_t_2));
__pyx_t_2 = 0;
/* "providers.pxd":254
/* "providers.pxd":261
* for index in range(inj_args_len):
* injection = <PositionalInjection>inj_args[index]
* positional_args.append(__get_value(injection)) # <<<<<<<<<<<<<<
* positional_args.extend(args)
*
*/
__pyx_t_2 = __pyx_f_19dependency_injector_9providers___get_value(((struct __pyx_obj_19dependency_injector_9providers_Injection *)__pyx_v_injection)); if (unlikely(!__pyx_t_2)) __PYX_ERR(1, 254, __pyx_L1_error)
__pyx_t_2 = __pyx_f_19dependency_injector_9providers___get_value(((struct __pyx_obj_19dependency_injector_9providers_Injection *)__pyx_v_injection)); if (unlikely(!__pyx_t_2)) __PYX_ERR(1, 261, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_2);
__pyx_t_6 = __Pyx_PyList_Append(__pyx_v_positional_args, __pyx_t_2); if (unlikely(__pyx_t_6 == ((int)-1))) __PYX_ERR(1, 254, __pyx_L1_error)
__pyx_t_6 = __Pyx_PyList_Append(__pyx_v_positional_args, __pyx_t_2); if (unlikely(__pyx_t_6 == ((int)-1))) __PYX_ERR(1, 261, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
}
/* "providers.pxd":255
/* "providers.pxd":262
* injection = <PositionalInjection>inj_args[index]
* positional_args.append(__get_value(injection))
* positional_args.extend(args) # <<<<<<<<<<<<<<
*
* return tuple(positional_args)
*/
__pyx_t_6 = __Pyx_PyList_Extend(__pyx_v_positional_args, __pyx_v_args); if (unlikely(__pyx_t_6 == ((int)-1))) __PYX_ERR(1, 255, __pyx_L1_error)
__pyx_t_6 = __Pyx_PyList_Extend(__pyx_v_positional_args, __pyx_v_args); if (unlikely(__pyx_t_6 == ((int)-1))) __PYX_ERR(1, 262, __pyx_L1_error)
/* "providers.pxd":257
/* "providers.pxd":264
* positional_args.extend(args)
*
* return tuple(positional_args) # <<<<<<<<<<<<<<
@ -9568,13 +9598,13 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___provid
*
*/
__Pyx_XDECREF(__pyx_r);
__pyx_t_2 = PyList_AsTuple(__pyx_v_positional_args); if (unlikely(!__pyx_t_2)) __PYX_ERR(1, 257, __pyx_L1_error)
__pyx_t_2 = PyList_AsTuple(__pyx_v_positional_args); if (unlikely(!__pyx_t_2)) __PYX_ERR(1, 264, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_2);
__pyx_r = ((PyObject*)__pyx_t_2);
__pyx_t_2 = 0;
goto __pyx_L0;
/* "providers.pxd":241
/* "providers.pxd":248
* @cython.boundscheck(False)
* @cython.wraparound(False)
* cdef inline tuple __provide_positional_args(tuple args, # <<<<<<<<<<<<<<
@ -9595,7 +9625,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___provid
return __pyx_r;
}
/* "providers.pxd":262
/* "providers.pxd":269
* @cython.boundscheck(False)
* @cython.wraparound(False)
* cdef inline dict __provide_keyword_args(dict kwargs, # <<<<<<<<<<<<<<
@ -9621,7 +9651,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___provid
int __pyx_clineno = 0;
__Pyx_RefNannySetupContext("__provide_keyword_args", 0);
/* "providers.pxd":269
/* "providers.pxd":276
* cdef NamedInjection kw_injection
*
* if len(kwargs) == 0: # <<<<<<<<<<<<<<
@ -9630,13 +9660,13 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___provid
*/
if (unlikely(__pyx_v_kwargs == Py_None)) {
PyErr_SetString(PyExc_TypeError, "object of type 'NoneType' has no len()");
__PYX_ERR(1, 269, __pyx_L1_error)
__PYX_ERR(1, 276, __pyx_L1_error)
}
__pyx_t_1 = PyDict_Size(__pyx_v_kwargs); if (unlikely(__pyx_t_1 == ((Py_ssize_t)-1))) __PYX_ERR(1, 269, __pyx_L1_error)
__pyx_t_1 = PyDict_Size(__pyx_v_kwargs); if (unlikely(__pyx_t_1 == ((Py_ssize_t)-1))) __PYX_ERR(1, 276, __pyx_L1_error)
__pyx_t_2 = ((__pyx_t_1 == 0) != 0);
if (__pyx_t_2) {
/* "providers.pxd":270
/* "providers.pxd":277
*
* if len(kwargs) == 0:
* for index in range(inj_kwargs_len): # <<<<<<<<<<<<<<
@ -9648,7 +9678,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___provid
for (__pyx_t_5 = 0; __pyx_t_5 < __pyx_t_4; __pyx_t_5+=1) {
__pyx_v_index = __pyx_t_5;
/* "providers.pxd":271
/* "providers.pxd":278
* if len(kwargs) == 0:
* for index in range(inj_kwargs_len):
* kw_injection = <NamedInjection>inj_kwargs[index] # <<<<<<<<<<<<<<
@ -9657,43 +9687,43 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___provid
*/
if (unlikely(__pyx_v_inj_kwargs == Py_None)) {
PyErr_SetString(PyExc_TypeError, "'NoneType' object is not subscriptable");
__PYX_ERR(1, 271, __pyx_L1_error)
__PYX_ERR(1, 278, __pyx_L1_error)
}
__pyx_t_6 = PyTuple_GET_ITEM(__pyx_v_inj_kwargs, __pyx_v_index);
__Pyx_INCREF(__pyx_t_6);
__Pyx_XDECREF_SET(__pyx_v_kw_injection, ((struct __pyx_obj_19dependency_injector_9providers_NamedInjection *)__pyx_t_6));
__pyx_t_6 = 0;
/* "providers.pxd":272
/* "providers.pxd":279
* for index in range(inj_kwargs_len):
* kw_injection = <NamedInjection>inj_kwargs[index]
* name = __get_name(kw_injection) # <<<<<<<<<<<<<<
* kwargs[name] = __get_value(kw_injection)
* else:
*/
__pyx_t_6 = __pyx_f_19dependency_injector_9providers___get_name(__pyx_v_kw_injection); if (unlikely(!__pyx_t_6)) __PYX_ERR(1, 272, __pyx_L1_error)
__pyx_t_6 = __pyx_f_19dependency_injector_9providers___get_name(__pyx_v_kw_injection); if (unlikely(!__pyx_t_6)) __PYX_ERR(1, 279, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_6);
__Pyx_XDECREF_SET(__pyx_v_name, __pyx_t_6);
__pyx_t_6 = 0;
/* "providers.pxd":273
/* "providers.pxd":280
* kw_injection = <NamedInjection>inj_kwargs[index]
* name = __get_name(kw_injection)
* kwargs[name] = __get_value(kw_injection) # <<<<<<<<<<<<<<
* else:
* for index in range(inj_kwargs_len):
*/
__pyx_t_6 = __pyx_f_19dependency_injector_9providers___get_value(((struct __pyx_obj_19dependency_injector_9providers_Injection *)__pyx_v_kw_injection)); if (unlikely(!__pyx_t_6)) __PYX_ERR(1, 273, __pyx_L1_error)
__pyx_t_6 = __pyx_f_19dependency_injector_9providers___get_value(((struct __pyx_obj_19dependency_injector_9providers_Injection *)__pyx_v_kw_injection)); if (unlikely(!__pyx_t_6)) __PYX_ERR(1, 280, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_6);
if (unlikely(__pyx_v_kwargs == Py_None)) {
PyErr_SetString(PyExc_TypeError, "'NoneType' object is not subscriptable");
__PYX_ERR(1, 273, __pyx_L1_error)
__PYX_ERR(1, 280, __pyx_L1_error)
}
if (unlikely(PyDict_SetItem(__pyx_v_kwargs, __pyx_v_name, __pyx_t_6) < 0)) __PYX_ERR(1, 273, __pyx_L1_error)
if (unlikely(PyDict_SetItem(__pyx_v_kwargs, __pyx_v_name, __pyx_t_6) < 0)) __PYX_ERR(1, 280, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
}
/* "providers.pxd":269
/* "providers.pxd":276
* cdef NamedInjection kw_injection
*
* if len(kwargs) == 0: # <<<<<<<<<<<<<<
@ -9703,7 +9733,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___provid
goto __pyx_L3;
}
/* "providers.pxd":275
/* "providers.pxd":282
* kwargs[name] = __get_value(kw_injection)
* else:
* for index in range(inj_kwargs_len): # <<<<<<<<<<<<<<
@ -9716,7 +9746,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___provid
for (__pyx_t_5 = 0; __pyx_t_5 < __pyx_t_4; __pyx_t_5+=1) {
__pyx_v_index = __pyx_t_5;
/* "providers.pxd":276
/* "providers.pxd":283
* else:
* for index in range(inj_kwargs_len):
* kw_injection = <NamedInjection>inj_kwargs[index] # <<<<<<<<<<<<<<
@ -9725,26 +9755,26 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___provid
*/
if (unlikely(__pyx_v_inj_kwargs == Py_None)) {
PyErr_SetString(PyExc_TypeError, "'NoneType' object is not subscriptable");
__PYX_ERR(1, 276, __pyx_L1_error)
__PYX_ERR(1, 283, __pyx_L1_error)
}
__pyx_t_6 = PyTuple_GET_ITEM(__pyx_v_inj_kwargs, __pyx_v_index);
__Pyx_INCREF(__pyx_t_6);
__Pyx_XDECREF_SET(__pyx_v_kw_injection, ((struct __pyx_obj_19dependency_injector_9providers_NamedInjection *)__pyx_t_6));
__pyx_t_6 = 0;
/* "providers.pxd":277
/* "providers.pxd":284
* for index in range(inj_kwargs_len):
* kw_injection = <NamedInjection>inj_kwargs[index]
* name = __get_name(kw_injection) # <<<<<<<<<<<<<<
* if name not in kwargs:
* kwargs[name] = __get_value(kw_injection)
*/
__pyx_t_6 = __pyx_f_19dependency_injector_9providers___get_name(__pyx_v_kw_injection); if (unlikely(!__pyx_t_6)) __PYX_ERR(1, 277, __pyx_L1_error)
__pyx_t_6 = __pyx_f_19dependency_injector_9providers___get_name(__pyx_v_kw_injection); if (unlikely(!__pyx_t_6)) __PYX_ERR(1, 284, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_6);
__Pyx_XDECREF_SET(__pyx_v_name, __pyx_t_6);
__pyx_t_6 = 0;
/* "providers.pxd":278
/* "providers.pxd":285
* kw_injection = <NamedInjection>inj_kwargs[index]
* name = __get_name(kw_injection)
* if name not in kwargs: # <<<<<<<<<<<<<<
@ -9753,29 +9783,29 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___provid
*/
if (unlikely(__pyx_v_kwargs == Py_None)) {
PyErr_SetString(PyExc_TypeError, "'NoneType' object is not iterable");
__PYX_ERR(1, 278, __pyx_L1_error)
__PYX_ERR(1, 285, __pyx_L1_error)
}
__pyx_t_2 = (__Pyx_PyDict_ContainsTF(__pyx_v_name, __pyx_v_kwargs, Py_NE)); if (unlikely(__pyx_t_2 < 0)) __PYX_ERR(1, 278, __pyx_L1_error)
__pyx_t_2 = (__Pyx_PyDict_ContainsTF(__pyx_v_name, __pyx_v_kwargs, Py_NE)); if (unlikely(__pyx_t_2 < 0)) __PYX_ERR(1, 285, __pyx_L1_error)
__pyx_t_7 = (__pyx_t_2 != 0);
if (__pyx_t_7) {
/* "providers.pxd":279
/* "providers.pxd":286
* name = __get_name(kw_injection)
* if name not in kwargs:
* kwargs[name] = __get_value(kw_injection) # <<<<<<<<<<<<<<
*
* return kwargs
*/
__pyx_t_6 = __pyx_f_19dependency_injector_9providers___get_value(((struct __pyx_obj_19dependency_injector_9providers_Injection *)__pyx_v_kw_injection)); if (unlikely(!__pyx_t_6)) __PYX_ERR(1, 279, __pyx_L1_error)
__pyx_t_6 = __pyx_f_19dependency_injector_9providers___get_value(((struct __pyx_obj_19dependency_injector_9providers_Injection *)__pyx_v_kw_injection)); if (unlikely(!__pyx_t_6)) __PYX_ERR(1, 286, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_6);
if (unlikely(__pyx_v_kwargs == Py_None)) {
PyErr_SetString(PyExc_TypeError, "'NoneType' object is not subscriptable");
__PYX_ERR(1, 279, __pyx_L1_error)
__PYX_ERR(1, 286, __pyx_L1_error)
}
if (unlikely(PyDict_SetItem(__pyx_v_kwargs, __pyx_v_name, __pyx_t_6) < 0)) __PYX_ERR(1, 279, __pyx_L1_error)
if (unlikely(PyDict_SetItem(__pyx_v_kwargs, __pyx_v_name, __pyx_t_6) < 0)) __PYX_ERR(1, 286, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
/* "providers.pxd":278
/* "providers.pxd":285
* kw_injection = <NamedInjection>inj_kwargs[index]
* name = __get_name(kw_injection)
* if name not in kwargs: # <<<<<<<<<<<<<<
@ -9787,7 +9817,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___provid
}
__pyx_L3:;
/* "providers.pxd":281
/* "providers.pxd":288
* kwargs[name] = __get_value(kw_injection)
*
* return kwargs # <<<<<<<<<<<<<<
@ -9799,7 +9829,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___provid
__pyx_r = __pyx_v_kwargs;
goto __pyx_L0;
/* "providers.pxd":262
/* "providers.pxd":269
* @cython.boundscheck(False)
* @cython.wraparound(False)
* cdef inline dict __provide_keyword_args(dict kwargs, # <<<<<<<<<<<<<<
@ -9820,7 +9850,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___provid
return __pyx_r;
}
/* "providers.pxd":286
/* "providers.pxd":293
* @cython.boundscheck(False)
* @cython.wraparound(False)
* cdef inline object __inject_attributes(object instance, # <<<<<<<<<<<<<<
@ -9844,7 +9874,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___inject
int __pyx_clineno = 0;
__Pyx_RefNannySetupContext("__inject_attributes", 0);
/* "providers.pxd":290
/* "providers.pxd":297
* int attributes_len):
* cdef NamedInjection attr_injection
* for index in range(attributes_len): # <<<<<<<<<<<<<<
@ -9856,7 +9886,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___inject
for (__pyx_t_3 = 0; __pyx_t_3 < __pyx_t_2; __pyx_t_3+=1) {
__pyx_v_index = __pyx_t_3;
/* "providers.pxd":291
/* "providers.pxd":298
* cdef NamedInjection attr_injection
* for index in range(attributes_len):
* attr_injection = <NamedInjection>attributes[index] # <<<<<<<<<<<<<<
@ -9865,46 +9895,46 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___inject
*/
if (unlikely(__pyx_v_attributes == Py_None)) {
PyErr_SetString(PyExc_TypeError, "'NoneType' object is not subscriptable");
__PYX_ERR(1, 291, __pyx_L1_error)
__PYX_ERR(1, 298, __pyx_L1_error)
}
__pyx_t_4 = PyTuple_GET_ITEM(__pyx_v_attributes, __pyx_v_index);
__Pyx_INCREF(__pyx_t_4);
__Pyx_XDECREF_SET(__pyx_v_attr_injection, ((struct __pyx_obj_19dependency_injector_9providers_NamedInjection *)__pyx_t_4));
__pyx_t_4 = 0;
/* "providers.pxd":293
/* "providers.pxd":300
* attr_injection = <NamedInjection>attributes[index]
* setattr(instance,
* __get_name(attr_injection), # <<<<<<<<<<<<<<
* __get_value(attr_injection))
*
*/
__pyx_t_4 = __pyx_f_19dependency_injector_9providers___get_name(__pyx_v_attr_injection); if (unlikely(!__pyx_t_4)) __PYX_ERR(1, 293, __pyx_L1_error)
__pyx_t_4 = __pyx_f_19dependency_injector_9providers___get_name(__pyx_v_attr_injection); if (unlikely(!__pyx_t_4)) __PYX_ERR(1, 300, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_4);
/* "providers.pxd":294
/* "providers.pxd":301
* setattr(instance,
* __get_name(attr_injection),
* __get_value(attr_injection)) # <<<<<<<<<<<<<<
*
*
*/
__pyx_t_5 = __pyx_f_19dependency_injector_9providers___get_value(((struct __pyx_obj_19dependency_injector_9providers_Injection *)__pyx_v_attr_injection)); if (unlikely(!__pyx_t_5)) __PYX_ERR(1, 294, __pyx_L1_error)
__pyx_t_5 = __pyx_f_19dependency_injector_9providers___get_value(((struct __pyx_obj_19dependency_injector_9providers_Injection *)__pyx_v_attr_injection)); if (unlikely(!__pyx_t_5)) __PYX_ERR(1, 301, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_5);
/* "providers.pxd":292
/* "providers.pxd":299
* for index in range(attributes_len):
* attr_injection = <NamedInjection>attributes[index]
* setattr(instance, # <<<<<<<<<<<<<<
* __get_name(attr_injection),
* __get_value(attr_injection))
*/
__pyx_t_6 = PyObject_SetAttr(__pyx_v_instance, __pyx_t_4, __pyx_t_5); if (unlikely(__pyx_t_6 == ((int)-1))) __PYX_ERR(1, 292, __pyx_L1_error)
__pyx_t_6 = PyObject_SetAttr(__pyx_v_instance, __pyx_t_4, __pyx_t_5); if (unlikely(__pyx_t_6 == ((int)-1))) __PYX_ERR(1, 299, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
__Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
}
/* "providers.pxd":286
/* "providers.pxd":293
* @cython.boundscheck(False)
* @cython.wraparound(False)
* cdef inline object __inject_attributes(object instance, # <<<<<<<<<<<<<<
@ -9927,7 +9957,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___inject
return __pyx_r;
}
/* "providers.pxd":297
/* "providers.pxd":304
*
*
* cdef inline object __callable_call(Callable self, tuple args, dict kwargs): # <<<<<<<<<<<<<<
@ -9947,7 +9977,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___callab
int __pyx_clineno = 0;
__Pyx_RefNannySetupContext("__callable_call", 0);
/* "providers.pxd":302
/* "providers.pxd":309
*
* positional_args = __provide_positional_args(args,
* self.__args, # <<<<<<<<<<<<<<
@ -9957,20 +9987,20 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___callab
__pyx_t_1 = __pyx_v_self->__pyx___args;
__Pyx_INCREF(__pyx_t_1);
/* "providers.pxd":301
/* "providers.pxd":308
* cdef dict keyword_args
*
* positional_args = __provide_positional_args(args, # <<<<<<<<<<<<<<
* self.__args,
* self.__args_len)
*/
__pyx_t_2 = __pyx_f_19dependency_injector_9providers___provide_positional_args(__pyx_v_args, ((PyObject*)__pyx_t_1), __pyx_v_self->__pyx___args_len); if (unlikely(!__pyx_t_2)) __PYX_ERR(1, 301, __pyx_L1_error)
__pyx_t_2 = __pyx_f_19dependency_injector_9providers___provide_positional_args(__pyx_v_args, ((PyObject*)__pyx_t_1), __pyx_v_self->__pyx___args_len); if (unlikely(!__pyx_t_2)) __PYX_ERR(1, 308, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_2);
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
__pyx_v_positional_args = ((PyObject*)__pyx_t_2);
__pyx_t_2 = 0;
/* "providers.pxd":305
/* "providers.pxd":312
* self.__args_len)
* keyword_args = __provide_keyword_args(kwargs,
* self.__kwargs, # <<<<<<<<<<<<<<
@ -9980,20 +10010,20 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___callab
__pyx_t_2 = __pyx_v_self->__pyx___kwargs;
__Pyx_INCREF(__pyx_t_2);
/* "providers.pxd":304
/* "providers.pxd":311
* self.__args,
* self.__args_len)
* keyword_args = __provide_keyword_args(kwargs, # <<<<<<<<<<<<<<
* self.__kwargs,
* self.__kwargs_len)
*/
__pyx_t_1 = __pyx_f_19dependency_injector_9providers___provide_keyword_args(__pyx_v_kwargs, ((PyObject*)__pyx_t_2), __pyx_v_self->__pyx___kwargs_len); if (unlikely(!__pyx_t_1)) __PYX_ERR(1, 304, __pyx_L1_error)
__pyx_t_1 = __pyx_f_19dependency_injector_9providers___provide_keyword_args(__pyx_v_kwargs, ((PyObject*)__pyx_t_2), __pyx_v_self->__pyx___kwargs_len); if (unlikely(!__pyx_t_1)) __PYX_ERR(1, 311, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
__pyx_v_keyword_args = ((PyObject*)__pyx_t_1);
__pyx_t_1 = 0;
/* "providers.pxd":308
/* "providers.pxd":315
* self.__kwargs_len)
*
* return self.__provides(*positional_args, **keyword_args) # <<<<<<<<<<<<<<
@ -10003,19 +10033,19 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___callab
__Pyx_XDECREF(__pyx_r);
if (unlikely(__pyx_v_positional_args == Py_None)) {
PyErr_SetString(PyExc_TypeError, "'NoneType' object is not iterable");
__PYX_ERR(1, 308, __pyx_L1_error)
__PYX_ERR(1, 315, __pyx_L1_error)
}
if (unlikely(__pyx_v_keyword_args == Py_None)) {
PyErr_SetString(PyExc_TypeError, "argument after ** must be a mapping, not NoneType");
__PYX_ERR(1, 308, __pyx_L1_error)
__PYX_ERR(1, 315, __pyx_L1_error)
}
__pyx_t_1 = __Pyx_PyObject_Call(__pyx_v_self->__pyx___provides, __pyx_v_positional_args, __pyx_v_keyword_args); if (unlikely(!__pyx_t_1)) __PYX_ERR(1, 308, __pyx_L1_error)
__pyx_t_1 = __Pyx_PyObject_Call(__pyx_v_self->__pyx___provides, __pyx_v_positional_args, __pyx_v_keyword_args); if (unlikely(!__pyx_t_1)) __PYX_ERR(1, 315, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__pyx_r = __pyx_t_1;
__pyx_t_1 = 0;
goto __pyx_L0;
/* "providers.pxd":297
/* "providers.pxd":304
*
*
* cdef inline object __callable_call(Callable self, tuple args, dict kwargs): # <<<<<<<<<<<<<<
@ -10037,7 +10067,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___callab
return __pyx_r;
}
/* "providers.pxd":311
/* "providers.pxd":318
*
*
* cdef inline object __factory_call(Factory self, tuple args, dict kwargs): # <<<<<<<<<<<<<<
@ -10057,7 +10087,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___factor
int __pyx_clineno = 0;
__Pyx_RefNannySetupContext("__factory_call", 0);
/* "providers.pxd":314
/* "providers.pxd":321
* cdef object instance
*
* instance = __callable_call(self.__instantiator, args, kwargs) # <<<<<<<<<<<<<<
@ -10066,13 +10096,13 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___factor
*/
__pyx_t_1 = ((PyObject *)__pyx_v_self->__pyx___instantiator);
__Pyx_INCREF(__pyx_t_1);
__pyx_t_2 = __pyx_f_19dependency_injector_9providers___callable_call(((struct __pyx_obj_19dependency_injector_9providers_Callable *)__pyx_t_1), __pyx_v_args, __pyx_v_kwargs); if (unlikely(!__pyx_t_2)) __PYX_ERR(1, 314, __pyx_L1_error)
__pyx_t_2 = __pyx_f_19dependency_injector_9providers___callable_call(((struct __pyx_obj_19dependency_injector_9providers_Callable *)__pyx_t_1), __pyx_v_args, __pyx_v_kwargs); if (unlikely(!__pyx_t_2)) __PYX_ERR(1, 321, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_2);
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
__pyx_v_instance = __pyx_t_2;
__pyx_t_2 = 0;
/* "providers.pxd":316
/* "providers.pxd":323
* instance = __callable_call(self.__instantiator, args, kwargs)
*
* if self.__attributes_len > 0: # <<<<<<<<<<<<<<
@ -10082,7 +10112,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___factor
__pyx_t_3 = ((__pyx_v_self->__pyx___attributes_len > 0) != 0);
if (__pyx_t_3) {
/* "providers.pxd":318
/* "providers.pxd":325
* if self.__attributes_len > 0:
* __inject_attributes(instance,
* self.__attributes, # <<<<<<<<<<<<<<
@ -10092,19 +10122,19 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___factor
__pyx_t_2 = __pyx_v_self->__pyx___attributes;
__Pyx_INCREF(__pyx_t_2);
/* "providers.pxd":317
/* "providers.pxd":324
*
* if self.__attributes_len > 0:
* __inject_attributes(instance, # <<<<<<<<<<<<<<
* self.__attributes,
* self.__attributes_len)
*/
__pyx_t_1 = __pyx_f_19dependency_injector_9providers___inject_attributes(__pyx_v_instance, ((PyObject*)__pyx_t_2), __pyx_v_self->__pyx___attributes_len); if (unlikely(!__pyx_t_1)) __PYX_ERR(1, 317, __pyx_L1_error)
__pyx_t_1 = __pyx_f_19dependency_injector_9providers___inject_attributes(__pyx_v_instance, ((PyObject*)__pyx_t_2), __pyx_v_self->__pyx___attributes_len); if (unlikely(!__pyx_t_1)) __PYX_ERR(1, 324, __pyx_L1_error)
__Pyx_GOTREF(__pyx_t_1);
__Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
/* "providers.pxd":316
/* "providers.pxd":323
* instance = __callable_call(self.__instantiator, args, kwargs)
*
* if self.__attributes_len > 0: # <<<<<<<<<<<<<<
@ -10113,7 +10143,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___factor
*/
}
/* "providers.pxd":321
/* "providers.pxd":328
* self.__attributes_len)
*
* return instance # <<<<<<<<<<<<<<
@ -10123,7 +10153,7 @@ static CYTHON_INLINE PyObject *__pyx_f_19dependency_injector_9providers___factor
__pyx_r = __pyx_v_instance;
goto __pyx_L0;
/* "providers.pxd":311
/* "providers.pxd":318
*
*
* cdef inline object __factory_call(Factory self, tuple args, dict kwargs): # <<<<<<<<<<<<<<
@ -11015,7 +11045,7 @@ static CYTHON_SMALL_CODE int __Pyx_InitCachedBuiltins(void) {
__pyx_builtin_super = __Pyx_GetBuiltinName(__pyx_n_s_super); if (!__pyx_builtin_super) __PYX_ERR(0, 55, __pyx_L1_error)
__pyx_builtin_id = __Pyx_GetBuiltinName(__pyx_n_s_id); if (!__pyx_builtin_id) __PYX_ERR(0, 59, __pyx_L1_error)
__pyx_builtin_AttributeError = __Pyx_GetBuiltinName(__pyx_n_s_AttributeError); if (!__pyx_builtin_AttributeError) __PYX_ERR(0, 138, __pyx_L1_error)
__pyx_builtin_range = __Pyx_GetBuiltinName(__pyx_n_s_range); if (!__pyx_builtin_range) __PYX_ERR(1, 252, __pyx_L1_error)
__pyx_builtin_range = __Pyx_GetBuiltinName(__pyx_n_s_range); if (!__pyx_builtin_range) __PYX_ERR(1, 259, __pyx_L1_error)
return 0;
__pyx_L1_error:;
return -1;
@ -11519,12 +11549,15 @@ static int __Pyx_modinit_type_import_code(void) {
__pyx_ptype_19dependency_injector_9providers_Container = __Pyx_ImportType(__pyx_t_1, "dependency_injector.providers", "Container", sizeof(struct __pyx_obj_19dependency_injector_9providers_Container), __Pyx_ImportType_CheckSize_Warn);
if (!__pyx_ptype_19dependency_injector_9providers_Container) __PYX_ERR(1, 179, __pyx_L1_error)
__pyx_vtabptr_19dependency_injector_9providers_Container = (struct __pyx_vtabstruct_19dependency_injector_9providers_Container*)__Pyx_GetVtable(__pyx_ptype_19dependency_injector_9providers_Container->tp_dict); if (unlikely(!__pyx_vtabptr_19dependency_injector_9providers_Container)) __PYX_ERR(1, 179, __pyx_L1_error)
__pyx_ptype_19dependency_injector_9providers_Selector = __Pyx_ImportType(__pyx_t_1, "dependency_injector.providers", "Selector", sizeof(struct __pyx_obj_19dependency_injector_9providers_Selector), __Pyx_ImportType_CheckSize_Warn);
if (!__pyx_ptype_19dependency_injector_9providers_Selector) __PYX_ERR(1, 187, __pyx_L1_error)
__pyx_vtabptr_19dependency_injector_9providers_Selector = (struct __pyx_vtabstruct_19dependency_injector_9providers_Selector*)__Pyx_GetVtable(__pyx_ptype_19dependency_injector_9providers_Selector->tp_dict); if (unlikely(!__pyx_vtabptr_19dependency_injector_9providers_Selector)) __PYX_ERR(1, 187, __pyx_L1_error)
__pyx_ptype_19dependency_injector_9providers_Injection = __Pyx_ImportType(__pyx_t_1, "dependency_injector.providers", "Injection", sizeof(struct __pyx_obj_19dependency_injector_9providers_Injection), __Pyx_ImportType_CheckSize_Warn);
if (!__pyx_ptype_19dependency_injector_9providers_Injection) __PYX_ERR(1, 188, __pyx_L1_error)
if (!__pyx_ptype_19dependency_injector_9providers_Injection) __PYX_ERR(1, 195, __pyx_L1_error)
__pyx_ptype_19dependency_injector_9providers_PositionalInjection = __Pyx_ImportType(__pyx_t_1, "dependency_injector.providers", "PositionalInjection", sizeof(struct __pyx_obj_19dependency_injector_9providers_PositionalInjection), __Pyx_ImportType_CheckSize_Warn);
if (!__pyx_ptype_19dependency_injector_9providers_PositionalInjection) __PYX_ERR(1, 195, __pyx_L1_error)
if (!__pyx_ptype_19dependency_injector_9providers_PositionalInjection) __PYX_ERR(1, 202, __pyx_L1_error)
__pyx_ptype_19dependency_injector_9providers_NamedInjection = __Pyx_ImportType(__pyx_t_1, "dependency_injector.providers", "NamedInjection", sizeof(struct __pyx_obj_19dependency_injector_9providers_NamedInjection), __Pyx_ImportType_CheckSize_Warn);
if (!__pyx_ptype_19dependency_injector_9providers_NamedInjection) __PYX_ERR(1, 199, __pyx_L1_error)
if (!__pyx_ptype_19dependency_injector_9providers_NamedInjection) __PYX_ERR(1, 206, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
__Pyx_RefNannyFinishContext();
return 0;
@ -12302,7 +12335,7 @@ if (!__Pyx_RefNanny) {
if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_2) < 0) __PYX_ERR(0, 1, __pyx_L1_error)
__Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
/* "providers.pxd":311
/* "providers.pxd":318
*
*
* cdef inline object __factory_call(Factory self, tuple args, dict kwargs): # <<<<<<<<<<<<<<

File diff suppressed because it is too large Load Diff

View File

@ -184,6 +184,13 @@ cdef class Container(Provider):
cpdef object _provide(self, tuple args, dict kwargs)
cdef class Selector(Provider):
cdef object __selector
cdef dict __providers
cpdef object _provide(self, tuple args, dict kwargs)
# Injections
cdef class Injection(object):
cdef object __value

View File

@ -1152,7 +1152,7 @@ cdef class Configuration(Object):
value = self.__call__()
if not isinstance(value, dict):
return
return overriding_context
for name in value.keys():
child_provider = self.__children.get(name)
@ -2267,6 +2267,103 @@ cdef class Container(Provider):
return self.container
cdef class Selector(Provider):
"""Selector provider selects provider based on the configuration value or other callable.
:py:class:`Selector` provider has a callable called ``selector`` and a dictionary of providers.
The ``selector`` callable is provided as a first positional argument. It can be
:py:class:`Configuration` provider or any other callable. It has to return a string value.
That value is used as a key for selecting the provider from the dictionary of providers.
The providers are provided as keyword arguments. Argument name is used as a key for
selecting the provider.
.. code-block:: python
config = Configuration()
selector = Selector(
config.one_or_another,
one=providers.Factory(SomeClass),
another=providers.Factory(SomeOtherClass),
)
config.override({'one_or_another': 'one'})
instance_1 = selector()
assert isinstance(instance_1, SomeClass)
config.override({'one_or_another': 'another'})
instance_2 = selector()
assert isinstance(instance_2, SomeOtherClass)
"""
def __init__(self, selector, **providers):
"""Initialize provider."""
self.__selector = selector
self.__providers = providers
super(Selector, self).__init__()
def __deepcopy__(self, memo):
"""Create and return full copy of provider."""
copied = memo.get(id(self))
if copied is not None:
return copied
copied = self.__class__(
deepcopy(self.__selector, memo),
**deepcopy(self.__providers, memo),
)
self._copy_overridings(copied, memo)
return copied
def __getattr__(self, name):
"""Return provider."""
if name.startswith('__') and name.endswith('__'):
raise AttributeError(
'\'{cls}\' object has no attribute '
'\'{attribute_name}\''.format(cls=self.__class__.__name__,
attribute_name=name))
if name not in self.__providers:
raise AttributeError('Selector has no "{0}" provider'.format(name))
return self.__providers[name]
def __str__(self):
"""Return string representation of provider.
:rtype: str
"""
return '<{provider}({selector}, {providers}) at {address}>'.format(
provider='.'.join(( self.__class__.__module__, self.__class__.__name__)),
selector=self.__selector,
providers=', '.join((
'{0}={1}'.format(name, provider)
for name, provider in self.__providers.items()
)),
address=hex(id(self)),
)
@property
def providers(self):
"""Return providers."""
return dict(self.__providers)
cpdef object _provide(self, tuple args, dict kwargs):
"""Return single instance."""
selector_value = self.__selector()
if selector_value is None:
raise Error('Selector value is undefined')
if selector_value not in self.__providers:
raise Error('Selector has no "{0}" provider'.format(selector_value))
return self.__providers[selector_value](*args, **kwargs)
cdef class Injection(object):
"""Abstract injection class."""

View File

@ -0,0 +1,150 @@
"""Dependency injector selector provider unit tests."""
import functools
import itertools
import sys
import unittest2 as unittest
from dependency_injector import providers
class SelectorTests(unittest.TestCase):
selector = providers.Configuration()
def test_is_provider(self):
self.assertTrue(providers.is_provider(providers.Selector(self.selector)))
def test_call(self):
provider = providers.Selector(
self.selector,
one=providers.Object(1),
two=providers.Object(2),
)
with self.selector.override('one'):
self.assertEqual(provider(), 1)
with self.selector.override('two'):
self.assertEqual(provider(), 2)
def test_call_any_callable(self):
provider = providers.Selector(
functools.partial(next, itertools.cycle(['one', 'two'])),
one=providers.Object(1),
two=providers.Object(2),
)
self.assertEqual(provider(), 1)
self.assertEqual(provider(), 2)
self.assertEqual(provider(), 1)
self.assertEqual(provider(), 2)
def test_call_with_context_args(self):
provider = providers.Selector(
self.selector,
one=providers.Callable(lambda *args, **kwargs: (args, kwargs)),
)
with self.selector.override('one'):
args, kwargs = provider(1, 2, three=3, four=4)
self.assertEqual(args, (1, 2))
self.assertEqual(kwargs, {'three': 3, 'four': 4})
def test_getattr(self):
provider_one = providers.Object(1)
provider_two = providers.Object(2)
provider = providers.Selector(
self.selector,
one=provider_one,
two=provider_two,
)
self.assertIs(provider.one, provider_one)
self.assertIs(provider.two, provider_two)
def test_call_overridden(self):
provider = providers.Selector(self.selector, sample=providers.Object(1))
overriding_provider1 = providers.Selector(self.selector, sample=providers.Object(2))
overriding_provider2 = providers.Selector(self.selector, sample=providers.Object(3))
provider.override(overriding_provider1)
provider.override(overriding_provider2)
with self.selector.override('sample'):
self.assertEqual(provider(), 3)
def test_deepcopy(self):
provider = providers.Selector(self.selector)
provider_copy = providers.deepcopy(provider)
self.assertIsNot(provider, provider_copy)
self.assertIsInstance(provider, providers.Selector)
def test_deepcopy_from_memo(self):
provider = providers.Selector(self.selector)
provider_copy_memo = providers.Selector(self.selector)
provider_copy = providers.deepcopy(
provider,
memo={id(provider): provider_copy_memo},
)
self.assertIs(provider_copy, provider_copy_memo)
def test_deepcopy_overridden(self):
provider = providers.Selector(self.selector)
object_provider = providers.Object(object())
provider.override(object_provider)
provider_copy = providers.deepcopy(provider)
object_provider_copy = provider_copy.overridden[0]
self.assertIsNot(provider, provider_copy)
self.assertIsInstance(provider, providers.Selector)
self.assertIsNot(object_provider, object_provider_copy)
self.assertIsInstance(object_provider_copy, providers.Object)
def test_deepcopy_with_sys_streams(self):
provider = providers.Selector(
self.selector,
stdin=providers.Object(sys.stdin),
stdout=providers.Object(sys.stdout),
stderr=providers.Object(sys.stderr),
)
provider_copy = providers.deepcopy(provider)
self.assertIsNot(provider, provider_copy)
self.assertIsInstance(provider_copy, providers.Selector)
with self.selector.override('stdin'):
self.assertIs(provider(), sys.stdin)
with self.selector.override('stdout'):
self.assertIs(provider(), sys.stdout)
with self.selector.override('stderr'):
self.assertIs(provider(), sys.stderr)
def test_repr(self):
provider = providers.Selector(
self.selector,
one=providers.Object(1),
two=providers.Object(2),
)
self.assertIn(
'<dependency_injector.providers.Selector({0}'.format(repr(self.selector)),
repr(provider),
)
self.assertIn('one={0}'.format(repr(provider.one)), repr(provider))
self.assertIn('two={0}'.format(repr(provider.two)), repr(provider))
self.assertIn('at {0}'.format(hex(id(provider))), repr(provider))