Skip to content

Commit 19a2f55

Browse files
committed
Update docs on creating custom providers with a requirement to specify .related property
1 parent 2fe0e00 commit 19a2f55

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

docs/main/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Development version
1313
- Fix ``container.reset_singleton()`` to reset all provider types, not only ``Singleton``.
1414
- Improve ``container.traverse(types=[...])`` and ``provider.traverse(types=[...])`` typing stubs
1515
to return ``types`` -typed iterator.
16+
- Update docs on creating custom providers with a requirement to specify ``.related`` property.
1617

1718
4.18.0
1819
------

docs/providers/custom.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ To create a custom provider you need to follow these rules:
2020
from the ``providers`` module. After the a new provider object is created use
2121
``Provider._copy_overriding()`` method to copy all overriding providers. See the example
2222
below.
23-
4. If the new provider has a ``__init__()`` method, it should call the parent
23+
4. If new provider has a ``__init__()`` method, it should call the parent
2424
``Provider.__init__()``.
25+
5. If new provider stores any other providers, these providers should be listed in
26+
``.related`` property. Property ``.related`` also should yield providers from parent
27+
``.related`` property.
2528

2629
.. literalinclude:: ../../examples/providers/custom_factory.py
2730
:language: python

examples/providers/custom_factory.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ def __deepcopy__(self, memo):
2525

2626
return copied
2727

28+
@property
29+
def related(self):
30+
"""Return related providers generator."""
31+
yield from [self._factory]
32+
yield from super().related
33+
2834
def _provide(self, args, kwargs):
2935
return self._factory(*args, **kwargs)
3036

0 commit comments

Comments
 (0)