From 19a2f551ae5ef48f03522175ffa40e15ea3b29e2 Mon Sep 17 00:00:00 2001 From: Roman Mogylatov Date: Fri, 5 Feb 2021 18:27:32 -0500 Subject: [PATCH] Update docs on creating custom providers with a requirement to specify ``.related`` property --- docs/main/changelog.rst | 1 + docs/providers/custom.rst | 5 ++++- examples/providers/custom_factory.py | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/docs/main/changelog.rst b/docs/main/changelog.rst index 95960b2e..d4563954 100644 --- a/docs/main/changelog.rst +++ b/docs/main/changelog.rst @@ -13,6 +13,7 @@ Development version - Fix ``container.reset_singleton()`` to reset all provider types, not only ``Singleton``. - Improve ``container.traverse(types=[...])`` and ``provider.traverse(types=[...])`` typing stubs to return ``types`` -typed iterator. +- Update docs on creating custom providers with a requirement to specify ``.related`` property. 4.18.0 ------ diff --git a/docs/providers/custom.rst b/docs/providers/custom.rst index 45fbcd2c..7838529c 100644 --- a/docs/providers/custom.rst +++ b/docs/providers/custom.rst @@ -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 ``Provider._copy_overriding()`` method to copy all overriding providers. See the example 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__()``. +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 :language: python diff --git a/examples/providers/custom_factory.py b/examples/providers/custom_factory.py index c73ab041..159e93af 100644 --- a/examples/providers/custom_factory.py +++ b/examples/providers/custom_factory.py @@ -25,6 +25,12 @@ class CustomFactory(providers.Provider): return copied + @property + def related(self): + """Return related providers generator.""" + yield from [self._factory] + yield from super().related + def _provide(self, args, kwargs): return self._factory(*args, **kwargs)