From d8d910d3420a2f8d5567c2d2b641805bafb7b1a5 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Mon, 8 May 2017 17:07:54 +0300 Subject: [PATCH] Update example of writing custom providers --- docs/main/changelog.rst | 1 + docs/providers/custom.rst | 4 +--- examples/providers/custom_factory.py | 14 +++++--------- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/docs/main/changelog.rst b/docs/main/changelog.rst index 59582743..d181ed2c 100644 --- a/docs/main/changelog.rst +++ b/docs/main/changelog.rst @@ -16,6 +16,7 @@ Development version - Add ``Provider.last_overriding`` read-only property that points to last overriding provider, if any. If target provider is not overridden, ``None`` would be returned. +- Update example of writing custom providers. 3.4.3 ----- diff --git a/docs/providers/custom.rst b/docs/providers/custom.rst index 6cfdcbc3..4f1d15f3 100644 --- a/docs/providers/custom.rst +++ b/docs/providers/custom.rst @@ -12,9 +12,7 @@ Below are some tips and recommendations that have to be met: 2. Cusom provider's ``__init__()`` could be overriden, but parent's initializer (:py:meth:`Provider.__init__`) has to be called. 3. Providing strategy has to be implemented in custom provider's - :py:meth:`Provider._provide` method. All ``*args`` & ``**kwargs`` - that will be recieved by :py:meth:`Provider.__call__` will be - transefed to custom provider's :py:meth:`Provider._provide`. + :py:meth:`Provider.__call__` method. 4. If custom provider is based on some standard providers, it is better to use delegation of standard providers, then extending of them. 5. If custom provider defines any attributes, it is good to list them in diff --git a/examples/providers/custom_factory.py b/examples/providers/custom_factory.py index f4b4ec26..8e978872 100644 --- a/examples/providers/custom_factory.py +++ b/examples/providers/custom_factory.py @@ -17,17 +17,13 @@ class UsersFactory(providers.Provider): self._factory = providers.Factory(User) super(UsersFactory, self).__init__() - def _provide(self, *args, **kwargs): - """Return provided instance. + def __call__(self, *args, **kwargs): + """Return provided object. - :param args: tuple of context positional arguments - :type args: tuple[object] - - :param kwargs: dictionary of context keyword arguments - :type kwargs: dict[str, object] - - :rtype: object + Callable interface implementation. """ + if self.last_overriding is not None: + return self.last_overriding._provide(args, kwargs) return self._factory(*args, **kwargs)