Update docs on creating custom providers with a requirement to specify `.related` property

This commit is contained in:
Roman Mogylatov 2021-02-05 18:27:32 -05:00
parent 2fe0e00cef
commit 19a2f551ae
3 changed files with 11 additions and 1 deletions

View File

@ -13,6 +13,7 @@ Development version
- Fix ``container.reset_singleton()`` to reset all provider types, not only ``Singleton``. - Fix ``container.reset_singleton()`` to reset all provider types, not only ``Singleton``.
- Improve ``container.traverse(types=[...])`` and ``provider.traverse(types=[...])`` typing stubs - Improve ``container.traverse(types=[...])`` and ``provider.traverse(types=[...])`` typing stubs
to return ``types`` -typed iterator. to return ``types`` -typed iterator.
- Update docs on creating custom providers with a requirement to specify ``.related`` property.
4.18.0 4.18.0
------ ------

View File

@ -20,8 +20,11 @@ To create a custom provider you need to follow these rules:
from the ``providers`` module. After the a new provider object is created use from the ``providers`` module. After the a new provider object is created use
``Provider._copy_overriding()`` method to copy all overriding providers. See the example ``Provider._copy_overriding()`` method to copy all overriding providers. See the example
below. below.
4. If the new provider has a ``__init__()`` method, it should call the parent 4. If new provider has a ``__init__()`` method, it should call the parent
``Provider.__init__()``. ``Provider.__init__()``.
5. If new provider stores any other providers, these providers should be listed in
``.related`` property. Property ``.related`` also should yield providers from parent
``.related`` property.
.. literalinclude:: ../../examples/providers/custom_factory.py .. literalinclude:: ../../examples/providers/custom_factory.py
:language: python :language: python

View File

@ -25,6 +25,12 @@ class CustomFactory(providers.Provider):
return copied return copied
@property
def related(self):
"""Return related providers generator."""
yield from [self._factory]
yield from super().related
def _provide(self, args, kwargs): def _provide(self, args, kwargs):
return self._factory(*args, **kwargs) return self._factory(*args, **kwargs)