Add container name to the representation of the dependency provider

This commit is contained in:
Roman Mogylatov 2021-02-14 19:09:08 -05:00
parent 0d8f2ff44e
commit 8b770772a1
4 changed files with 6045 additions and 5836 deletions

View File

@ -13,6 +13,7 @@ Development version
are defined. are defined.
See issue: `#383 <https://github.com/ets-labs/python-dependency-injector/issues/383>`_. See issue: `#383 <https://github.com/ets-labs/python-dependency-injector/issues/383>`_.
Thanks to `@shaunc <https://github.com/shaunc>`_ for suggesting the feature. Thanks to `@shaunc <https://github.com/shaunc>`_ for suggesting the feature.
- Add container name to the representation of the ``Dependency`` provider.
- Add docs cross-links between ``Singleton`` provider and "Reset container singletons" - Add docs cross-links between ``Singleton`` provider and "Reset container singletons"
pages. pages.

File diff suppressed because it is too large Load Diff

View File

@ -689,7 +689,12 @@ cdef class Dependency(Provider):
:rtype: str :rtype: str
""" """
return represent_provider(provider=self, provides=self.__instance_of) name = f'<{self.__class__.__module__}.{self.__class__.__name__}'
name += f'({repr(self.__instance_of)}) at {hex(id(self))}'
if self.parent_name:
name += f', container name: "{self.parent_name}"'
name += f'>'
return name
def __repr__(self): def __repr__(self):
"""Return string representation of provider. """Return string representation of provider.

View File

@ -591,6 +591,18 @@ class DependencyTests(unittest.TestCase):
repr(list), repr(list),
hex(id(self.provider)))) hex(id(self.provider))))
def test_repr_in_container(self):
class Container(containers.DeclarativeContainer):
dependency = providers.Dependency(instance_of=int)
container = Container()
self.assertEqual(repr(container.dependency),
'<dependency_injector.providers.'
'Dependency({0}) at {1}, container name: "Container.dependency">'.format(
repr(int),
hex(id(container.dependency))))
class ExternalDependencyTests(unittest.TestCase): class ExternalDependencyTests(unittest.TestCase):