Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 45 additions & 56 deletions src/dependency_injector/containers.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/dependency_injector/containers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ class DynamicContainer(Container):

return copied

def __setattr__(self, str name, object value):
def __setattr__(self, name, value):
"""Set instance attribute.

If value of attribute is provider, it will be added into providers
dictionary.

:param name: Attribute's name
:type name: str
:type name: object

:param value: Attribute's value
:type value: object
Expand All @@ -129,14 +129,14 @@ class DynamicContainer(Container):

super(DynamicContainer, self).__setattr__(name, value)

def __delattr__(self, str name):
def __delattr__(self, name):
"""Delete instance attribute.

If value of attribute is provider, it will be deleted from providers
dictionary.

:param name: Attribute's name
:type name: str
:type name: object

:rtype: None
"""
Expand Down Expand Up @@ -169,7 +169,7 @@ class DynamicContainer(Container):

:param providers: Dictionary of providers
:type providers:
dict[str, :py:class:`dependency_injector.providers.Provider`]
dict[object, :py:class:`dependency_injector.providers.Provider`]

:rtype: None
"""
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/containers/test_dynamic_py2_py3.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,3 +686,36 @@ class Container(containers.DeclarativeContainer):

self.assertIs(container.__self__(), container)
self.assertIs(container.sub_container().__self__(), container.sub_container())


class DynamicContainerWithCustomStringTests(unittest.TestCase):
# See: https://github.com/ets-labs/python-dependency-injector/issues/479

class CustomString(str):
pass

class CustomClass:
thing = None

def setUp(self):
self.object = self.CustomClass()
self.container = containers.DynamicContainer()
self.provider = providers.Provider()

def test_setattr(self):
setattr(self.container, self.CustomString('test_attr'), self.provider)
self.assertIs(self.container.test_attr, self.provider)

def test_delattr(self):
setattr(self.container, self.CustomString('test_attr'), self.provider)
delattr(self.container, self.CustomString('test_attr'))
with self.assertRaises(AttributeError):
self.container.test_attr

def test_set_provider(self):
self.container.set_provider(self.CustomString('test_attr'), self.provider)
self.assertIs(self.container.test_attr, self.provider)

def test_set_providers(self):
self.container.set_providers(**{self.CustomString('test_attr'): self.provider})
self.assertIs(self.container.test_attr, self.provider)