From a29508cb7aa021e9aaff4225d02bfedd300297c4 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Mon, 16 May 2016 11:16:14 +0300 Subject: [PATCH] Implement add_injection() for Callable, Factory & Singleton providers --- dependency_injector/providers/callable.py | 18 +++++++++++-- dependency_injector/providers/creational.py | 28 +++++++++++++++------ docs/main/changelog.rst | 6 +++++ 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/dependency_injector/providers/callable.py b/dependency_injector/providers/callable.py index 1a21f6ab..a69f70eb 100644 --- a/dependency_injector/providers/callable.py +++ b/dependency_injector/providers/callable.py @@ -76,8 +76,10 @@ class Callable(Provider): self.provides = provides - self.args = _parse_args_injections(args) - self.kwargs = _parse_kwargs_injections(args, kwargs) + self.args = tuple() + self.kwargs = tuple() + + self.add_injections(*args, **kwargs) super(Callable, self).__init__() @@ -89,6 +91,18 @@ class Callable(Provider): """ return self.args + self.kwargs + def add_injections(self, *args, **kwargs): + """Add provider injections. + + :param args: Tuple of injections. + :type args: tuple + + :param kwargs: Dictionary of injections. + :type kwargs: dict + """ + self.args += _parse_args_injections(args) + self.kwargs += _parse_kwargs_injections(args, kwargs) + def _provide(self, *args, **kwargs): """Return provided instance. diff --git a/dependency_injector/providers/creational.py b/dependency_injector/providers/creational.py index 06a2419c..a7c60bb7 100644 --- a/dependency_injector/providers/creational.py +++ b/dependency_injector/providers/creational.py @@ -106,13 +106,8 @@ class Factory(Callable): raise Error('{0} can provide only {1} instances'.format( self.__class__, self.__class__.provided_type)) - self.attributes = tuple(injection - for injection in args - if is_attribute_injection(injection)) - - self.methods = tuple(injection - for injection in args - if is_method_injection(injection)) + self.attributes = tuple() + self.methods = tuple() super(Factory, self).__init__(provides, *args, **kwargs) @@ -126,6 +121,25 @@ class Factory(Callable): """ return self.args + self.kwargs + self.attributes + self.methods + def add_injections(self, *args, **kwargs): + """Add provider injections. + + :param args: Tuple of injections. + :type args: tuple + + :param kwargs: Dictionary of injections. + :type kwargs: dict + """ + self.attributes += tuple(injection + for injection in args + if is_attribute_injection(injection)) + + self.methods += tuple(injection + for injection in args + if is_method_injection(injection)) + + super(Factory, self).add_injections(*args, **kwargs) + def _provide(self, *args, **kwargs): """Return provided instance. diff --git a/docs/main/changelog.rst b/docs/main/changelog.rst index 81ccf38a..5726767f 100644 --- a/docs/main/changelog.rst +++ b/docs/main/changelog.rst @@ -11,6 +11,12 @@ Development version ------------------- - No features. +1.17.0 +------ +- Add ``add_injections()`` method to ``Callable``, ``DelegatedCallable``, + ``Factory``, ``DelegatedFactory``, ``Singleton`` and ``DelegatedSingleton`` + providers. + 1.16.8 ------ - Fix some typos in introduction section of documentation.