Fix an issue with Dict provider non-string keys

This commit is contained in:
Roman Mogylatov 2021-03-30 10:25:45 -04:00
parent d04596be73
commit 7b70f46601
4 changed files with 3300 additions and 2924 deletions

View File

@ -7,6 +7,12 @@ that were made in every particular version.
From version 0.7.6 *Dependency Injector* framework strictly From version 0.7.6 *Dependency Injector* framework strictly
follows `Semantic versioning`_ follows `Semantic versioning`_
Development version
-------------------
- Fix an issue with ``Dict`` provider non-string keys.
See issue: `#435 <https://github.com/ets-labs/python-dependency-injector/issues/435>`_.
Thanks to `@daniel55411 <https://github.com/daniel55411>`_ for reporting the issue.
4.31.1 4.31.1
------ ------
- Fix ``ThreadSafeSingleton`` synchronization issue. - Fix ``ThreadSafeSingleton`` synchronization issue.

File diff suppressed because it is too large Load Diff

View File

@ -3169,7 +3169,7 @@ cdef class Dict(Provider):
return copied return copied
copied = _memorized_duplicate(self, memo) copied = _memorized_duplicate(self, memo)
copied.set_kwargs(**deepcopy(self.kwargs, memo)) self._copy_kwargs(copied, memo)
self._copy_overridings(copied, memo) self._copy_overridings(copied, memo)
return copied return copied
@ -3238,11 +3238,20 @@ cdef class Dict(Provider):
yield from filter(is_provider, self.kwargs.values()) yield from filter(is_provider, self.kwargs.values())
yield from super().related yield from super().related
def _copy_kwargs(self, copied, memo):
"""Return copy of kwargs."""
copied_kwargs = {
_copy_if_provider(name, memo): _copy_if_provider(value, memo)
for name, value in self.kwargs.items()
}
copied.set_kwargs(copied_kwargs)
cpdef object _provide(self, tuple args, dict kwargs): cpdef object _provide(self, tuple args, dict kwargs):
"""Return result of provided callable's call.""" """Return result of provided callable's call."""
return __provide_keyword_args(kwargs, self.__kwargs, self.__kwargs_len) return __provide_keyword_args(kwargs, self.__kwargs, self.__kwargs_len)
cdef class Resource(Provider): cdef class Resource(Provider):
"""Resource provider provides a component with initialization and shutdown.""" """Resource provider provides a component with initialization and shutdown."""

View File

@ -173,6 +173,27 @@ class DictTests(unittest.TestCase):
self.assertIs(dependent_provider2.cls, dependent_provider_copy2.cls) self.assertIs(dependent_provider2.cls, dependent_provider_copy2.cls)
self.assertIsNot(dependent_provider2, dependent_provider_copy2) self.assertIsNot(dependent_provider2, dependent_provider_copy2)
def test_deepcopy_kwargs_non_string_keys(self):
a1 = object()
a2 = object()
dependent_provider1 = providers.Factory(list)
dependent_provider2 = providers.Factory(dict)
provider = providers.Dict({a1: dependent_provider1, a2: dependent_provider2})
provider_copy = providers.deepcopy(provider)
dependent_provider_copy1 = provider_copy.kwargs[a1]
dependent_provider_copy2 = provider_copy.kwargs[a2]
self.assertNotEqual(provider.kwargs, provider_copy.kwargs)
self.assertIs(dependent_provider1.cls, dependent_provider_copy1.cls)
self.assertIsNot(dependent_provider1, dependent_provider_copy1)
self.assertIs(dependent_provider2.cls, dependent_provider_copy2.cls)
self.assertIsNot(dependent_provider2, dependent_provider_copy2)
def test_deepcopy_overridden(self): def test_deepcopy_overridden(self):
provider = providers.Dict() provider = providers.Dict()
object_provider = providers.Object(object()) object_provider = providers.Object(object())