This commit is contained in:
Roman Mogylatov 2022-01-28 23:12:08 -05:00
parent 8dc3dd2f09
commit 38ca1cdeed
4 changed files with 34 additions and 13 deletions

View File

@ -21,6 +21,10 @@ Development version
``FactoryAggregate.factories`` attribute. ``FactoryAggregate.factories`` attribute.
- Add ``.set_providers()`` method to the ``FactoryAggregate`` provider. It is an alias for - Add ``.set_providers()`` method to the ``FactoryAggregate`` provider. It is an alias for
``FactoryAggregate.set_factories()`` method. ``FactoryAggregate.set_factories()`` method.
- Fix ``Dependency`` provider to don't raise "Dependency is not defined" error when the ``default``
is a falsy value of proper type.
See issue `#550 <https://github.com/ets-labs/python-dependency-injector/issues/550>`_. Thanks to
`@approxit <https://github.com/approxit>`_ for reporting the issue.
- Refactor ``FactoryAggregate`` provider internals. - Refactor ``FactoryAggregate`` provider internals.
- Update logo on Github and in docs to support dark themes and remove some imperfections. - Update logo on Github and in docs to support dark themes and remove some imperfections.

View File

@ -19881,7 +19881,7 @@ static PyObject *__pyx_pf_19dependency_injector_9providers_10Dependency_7default
* *
* def set_default(self, default): # <<<<<<<<<<<<<< * def set_default(self, default): # <<<<<<<<<<<<<<
* """Set type.""" * """Set type."""
* if default and not isinstance(default, Provider): * if default is not None and not isinstance(default, Provider):
*/ */
/* Python wrapper */ /* Python wrapper */
@ -19914,25 +19914,26 @@ static PyObject *__pyx_pf_19dependency_injector_9providers_10Dependency_14set_de
/* "dependency_injector/providers.pyx":882 /* "dependency_injector/providers.pyx":882
* def set_default(self, default): * def set_default(self, default):
* """Set type.""" * """Set type."""
* if default and not isinstance(default, Provider): # <<<<<<<<<<<<<< * if default is not None and not isinstance(default, Provider): # <<<<<<<<<<<<<<
* default = Object(default) * default = Object(default)
* self.__default = default * self.__default = default
*/ */
__pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_v_default); if (unlikely(__pyx_t_2 < 0)) __PYX_ERR(1, 882, __pyx_L1_error) __pyx_t_2 = (__pyx_v_default != Py_None);
if (__pyx_t_2) { __pyx_t_3 = (__pyx_t_2 != 0);
if (__pyx_t_3) {
} else { } else {
__pyx_t_1 = __pyx_t_2; __pyx_t_1 = __pyx_t_3;
goto __pyx_L4_bool_binop_done; goto __pyx_L4_bool_binop_done;
} }
__pyx_t_2 = __Pyx_TypeCheck(__pyx_v_default, __pyx_ptype_19dependency_injector_9providers_Provider); __pyx_t_3 = __Pyx_TypeCheck(__pyx_v_default, __pyx_ptype_19dependency_injector_9providers_Provider);
__pyx_t_3 = ((!(__pyx_t_2 != 0)) != 0); __pyx_t_2 = ((!(__pyx_t_3 != 0)) != 0);
__pyx_t_1 = __pyx_t_3; __pyx_t_1 = __pyx_t_2;
__pyx_L4_bool_binop_done:; __pyx_L4_bool_binop_done:;
if (__pyx_t_1) { if (__pyx_t_1) {
/* "dependency_injector/providers.pyx":883 /* "dependency_injector/providers.pyx":883
* """Set type.""" * """Set type."""
* if default and not isinstance(default, Provider): * if default is not None and not isinstance(default, Provider):
* default = Object(default) # <<<<<<<<<<<<<< * default = Object(default) # <<<<<<<<<<<<<<
* self.__default = default * self.__default = default
* return self * return self
@ -19945,14 +19946,14 @@ static PyObject *__pyx_pf_19dependency_injector_9providers_10Dependency_14set_de
/* "dependency_injector/providers.pyx":882 /* "dependency_injector/providers.pyx":882
* def set_default(self, default): * def set_default(self, default):
* """Set type.""" * """Set type."""
* if default and not isinstance(default, Provider): # <<<<<<<<<<<<<< * if default is not None and not isinstance(default, Provider): # <<<<<<<<<<<<<<
* default = Object(default) * default = Object(default)
* self.__default = default * self.__default = default
*/ */
} }
/* "dependency_injector/providers.pyx":884 /* "dependency_injector/providers.pyx":884
* if default and not isinstance(default, Provider): * if default is not None and not isinstance(default, Provider):
* default = Object(default) * default = Object(default)
* self.__default = default # <<<<<<<<<<<<<< * self.__default = default # <<<<<<<<<<<<<<
* return self * return self
@ -19981,7 +19982,7 @@ static PyObject *__pyx_pf_19dependency_injector_9providers_10Dependency_14set_de
* *
* def set_default(self, default): # <<<<<<<<<<<<<< * def set_default(self, default): # <<<<<<<<<<<<<<
* """Set type.""" * """Set type."""
* if default and not isinstance(default, Provider): * if default is not None and not isinstance(default, Provider):
*/ */
/* function exit code */ /* function exit code */

View File

@ -879,7 +879,7 @@ cdef class Dependency(Provider):
def set_default(self, default): def set_default(self, default):
"""Set type.""" """Set type."""
if default and not isinstance(default, Provider): if default is not None and not isinstance(default, Provider):
default = Object(default) default = Object(default)
self.__default = default self.__default = default
return self return self

View File

@ -77,6 +77,22 @@ def test_default_attribute_provider():
assert provider.default is default assert provider.default is default
def test_default_with_empty_dict():
# See: https://github.com/ets-labs/python-dependency-injector/issues/550
default = {}
provider = providers.Dependency(instance_of=dict, default=default)
assert provider() == default
assert provider.default() == default
def test_default_with_empty_string():
# See: https://github.com/ets-labs/python-dependency-injector/issues/550
default = ""
provider = providers.Dependency(instance_of=str, default=default)
assert provider() == default
assert provider.default() == default
def test_is_defined(provider): def test_is_defined(provider):
assert provider.is_defined is False assert provider.is_defined is False