mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
Change style and get first stable run
This commit is contained in:
parent
2878ea5515
commit
f2a9b35c6d
|
@ -1,13 +1,15 @@
|
||||||
"""Injections module."""
|
"""Injections module."""
|
||||||
|
|
||||||
|
import itertools
|
||||||
|
|
||||||
import six
|
import six
|
||||||
|
|
||||||
from dependency_injector.utils import (
|
from dependency_injector.utils import (
|
||||||
is_provider,
|
is_provider,
|
||||||
is_delegated_provider,
|
|
||||||
is_injection,
|
is_injection,
|
||||||
is_arg_injection,
|
is_arg_injection,
|
||||||
is_kwarg_injection,
|
is_kwarg_injection,
|
||||||
|
is_delegated_provider,
|
||||||
fetch_cls_init,
|
fetch_cls_init,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -246,18 +248,20 @@ def inject(*args, **kwargs):
|
||||||
|
|
||||||
|
|
||||||
def _parse_args_injections(args):
|
def _parse_args_injections(args):
|
||||||
"""Parse positional argument injections according to current syntax."""
|
|
||||||
return tuple(Arg(arg) if not is_injection(arg) else arg
|
return tuple(Arg(arg) if not is_injection(arg) else arg
|
||||||
for arg in args
|
for arg in args
|
||||||
if not is_injection(arg) or is_arg_injection(arg))
|
if not is_injection(arg) or is_arg_injection(arg))
|
||||||
|
|
||||||
|
|
||||||
def _parse_kwargs_injections(args, kwargs):
|
def _parse_kwargs_injections(args, kwargs):
|
||||||
"""Parse keyword argument injections according to current syntax."""
|
|
||||||
kwarg_injections = tuple(injection
|
kwarg_injections = tuple(injection
|
||||||
for injection in args
|
for injection in args
|
||||||
if is_kwarg_injection(injection))
|
if is_kwarg_injection(injection))
|
||||||
if kwargs:
|
if kwargs:
|
||||||
kwarg_injections += tuple(KwArg(name, value)
|
kwarg_injections += tuple(itertools.starmap(KwArg,
|
||||||
for name, value in six.iteritems(kwargs))
|
six.iteritems(kwargs)))
|
||||||
return kwarg_injections
|
return kwarg_injections
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_attribute_injections(attributes):
|
||||||
|
return tuple(itertools.starmap(Attribute, six.iteritems(attributes)))
|
||||||
|
|
|
@ -13,27 +13,18 @@ from dependency_injector.errors import Error
|
||||||
|
|
||||||
@six.python_2_unicode_compatible
|
@six.python_2_unicode_compatible
|
||||||
class Callable(Provider):
|
class Callable(Provider):
|
||||||
""":py:class:`Callable` provider calls wrapped callable on every call.
|
r""":py:class:`Callable` provider calls wrapped callable on every call.
|
||||||
|
|
||||||
:py:class:`Callable` provider provides callable that is called on every
|
:py:class:`Callable` provider provides callable that is called on every
|
||||||
provider call with some predefined dependency injections.
|
provider call with some predefined dependency injections.
|
||||||
|
|
||||||
:py:class:`Callable` syntax of passing injections is the same like
|
:py:class:`Callable` supports positional and keyword argument injections:
|
||||||
:py:class:`Factory` one:
|
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
# simplified syntax for passing positional and keyword argument
|
some_function = Callable(some_function) \
|
||||||
# injections:
|
.args('arg1', 'arg2') \
|
||||||
some_function = Callable(some_function, 'arg1', 'arg2', arg3=3, arg4=4)
|
.kwargs(arg3=3, arg4=4)
|
||||||
|
|
||||||
# extended (full) syntax for passing positional and keyword argument
|
|
||||||
# injections:
|
|
||||||
some_function = Callable(some_function,
|
|
||||||
injections.Arg(1),
|
|
||||||
injections.Arg(2),
|
|
||||||
injections.KwArg('some_arg', 3),
|
|
||||||
injections.KwArg('other_arg', 4))
|
|
||||||
|
|
||||||
.. py:attribute:: provides
|
.. py:attribute:: provides
|
||||||
|
|
||||||
|
@ -41,32 +32,26 @@ class Callable(Provider):
|
||||||
|
|
||||||
:type: callable
|
:type: callable
|
||||||
|
|
||||||
.. py:attribute:: args
|
.. py:attribute:: _args
|
||||||
|
|
||||||
Tuple of positional argument injections.
|
Tuple of positional argument injections.
|
||||||
|
|
||||||
:type: tuple[:py:class:`dependency_injector.injections.Arg`]
|
:type: tuple[:py:class:`dependency_injector.injections.Arg`]
|
||||||
|
|
||||||
.. py:attribute:: kwargs
|
.. py:attribute:: _kwargs
|
||||||
|
|
||||||
Tuple of keyword argument injections.
|
Tuple of keyword argument injections.
|
||||||
|
|
||||||
:type: tuple[:py:class:`dependency_injector.injections.KwArg`]
|
:type: tuple[:py:class:`dependency_injector.injections.KwArg`]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__slots__ = ('provides', 'args', 'kwargs')
|
__slots__ = ('provides', '_args', '_kwargs')
|
||||||
|
|
||||||
def __init__(self, provides, *args, **kwargs):
|
def __init__(self, provides):
|
||||||
"""Initializer.
|
"""Initializer.
|
||||||
|
|
||||||
:param provides: Wrapped callable.
|
:param provides: Wrapped callable.
|
||||||
:type provides: callable
|
:type provides: callable
|
||||||
|
|
||||||
:param args: Tuple of injections.
|
|
||||||
:type args: tuple
|
|
||||||
|
|
||||||
:param kwargs: Dictionary of injections.
|
|
||||||
:type kwargs: dict
|
|
||||||
"""
|
"""
|
||||||
if not callable(provides):
|
if not callable(provides):
|
||||||
raise Error('Provider {0} expected to get callable, '
|
raise Error('Provider {0} expected to get callable, '
|
||||||
|
@ -76,10 +61,8 @@ class Callable(Provider):
|
||||||
|
|
||||||
self.provides = provides
|
self.provides = provides
|
||||||
|
|
||||||
self.args = tuple()
|
self._args = tuple()
|
||||||
self.kwargs = tuple()
|
self._kwargs = tuple()
|
||||||
|
|
||||||
self.add_injections(*args, **kwargs)
|
|
||||||
|
|
||||||
super(Callable, self).__init__()
|
super(Callable, self).__init__()
|
||||||
|
|
||||||
|
@ -89,19 +72,29 @@ class Callable(Provider):
|
||||||
|
|
||||||
:rtype: tuple[:py:class:`dependency_injector.injections.Injection`]
|
:rtype: tuple[:py:class:`dependency_injector.injections.Injection`]
|
||||||
"""
|
"""
|
||||||
return self.args + self.kwargs
|
return self._args + self._kwargs
|
||||||
|
|
||||||
def add_injections(self, *args, **kwargs):
|
def args(self, *args):
|
||||||
"""Add provider injections.
|
"""Add postional argument injections.
|
||||||
|
|
||||||
:param args: Tuple of injections.
|
:param args: Tuple of injections.
|
||||||
:type args: tuple
|
:type args: tuple
|
||||||
|
|
||||||
|
:return: Reference ``self``
|
||||||
|
"""
|
||||||
|
self._args += _parse_args_injections(args)
|
||||||
|
return self
|
||||||
|
|
||||||
|
def kwargs(self, **kwargs):
|
||||||
|
"""Add keyword argument injections.
|
||||||
|
|
||||||
:param kwargs: Dictionary of injections.
|
:param kwargs: Dictionary of injections.
|
||||||
:type kwargs: dict
|
:type kwargs: dict
|
||||||
|
|
||||||
|
:return: Reference ``self``
|
||||||
"""
|
"""
|
||||||
self.args += _parse_args_injections(args)
|
self._kwargs += _parse_kwargs_injections(tuple(), kwargs)
|
||||||
self.kwargs += _parse_kwargs_injections(args, kwargs)
|
return self
|
||||||
|
|
||||||
def _provide(self, *args, **kwargs):
|
def _provide(self, *args, **kwargs):
|
||||||
"""Return provided instance.
|
"""Return provided instance.
|
||||||
|
@ -114,10 +107,10 @@ class Callable(Provider):
|
||||||
|
|
||||||
:rtype: object
|
:rtype: object
|
||||||
"""
|
"""
|
||||||
if self.args:
|
if self._args:
|
||||||
args = tuple(arg.value for arg in self.args) + args
|
args = tuple(arg.value for arg in self._args) + args
|
||||||
|
|
||||||
for kwarg in self.kwargs:
|
for kwarg in self._kwargs:
|
||||||
if kwarg.name not in kwargs:
|
if kwarg.name not in kwargs:
|
||||||
kwargs[kwarg.name] = kwarg.value
|
kwargs[kwarg.name] = kwarg.value
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
"""Dependency injector creational providers."""
|
"""Dependency injector creational providers."""
|
||||||
|
|
||||||
from dependency_injector.providers.callable import Callable
|
from dependency_injector.providers.callable import Callable
|
||||||
from dependency_injector.utils import (
|
from dependency_injector.injections import _parse_attribute_injections
|
||||||
is_attribute_injection,
|
from dependency_injector.utils import GLOBAL_LOCK
|
||||||
GLOBAL_LOCK,
|
|
||||||
)
|
|
||||||
from dependency_injector.errors import Error
|
from dependency_injector.errors import Error
|
||||||
|
|
||||||
|
|
||||||
|
@ -79,7 +77,7 @@ class Factory(Callable):
|
||||||
|
|
||||||
provided_type = None
|
provided_type = None
|
||||||
|
|
||||||
__slots__ = ('cls', 'attributes')
|
__slots__ = ('cls', '_attributes')
|
||||||
|
|
||||||
def __init__(self, provides, *args, **kwargs):
|
def __init__(self, provides, *args, **kwargs):
|
||||||
"""Initializer.
|
"""Initializer.
|
||||||
|
@ -99,7 +97,7 @@ class Factory(Callable):
|
||||||
raise Error('{0} can provide only {1} instances'.format(
|
raise Error('{0} can provide only {1} instances'.format(
|
||||||
self.__class__, self.__class__.provided_type))
|
self.__class__, self.__class__.provided_type))
|
||||||
|
|
||||||
self.attributes = tuple()
|
self._attributes = tuple()
|
||||||
|
|
||||||
super(Factory, self).__init__(provides, *args, **kwargs)
|
super(Factory, self).__init__(provides, *args, **kwargs)
|
||||||
|
|
||||||
|
@ -111,22 +109,17 @@ class Factory(Callable):
|
||||||
|
|
||||||
:rtype: tuple[:py:class:`dependency_injector.injections.Injection`]
|
:rtype: tuple[:py:class:`dependency_injector.injections.Injection`]
|
||||||
"""
|
"""
|
||||||
return self.args + self.kwargs + self.attributes
|
return self._args + self._kwargs + self._attributes
|
||||||
|
|
||||||
def add_injections(self, *args, **kwargs):
|
def attributes(self, **kwargs):
|
||||||
"""Add provider injections.
|
"""Add attribute injections.
|
||||||
|
|
||||||
:param args: Tuple of injections.
|
|
||||||
:type args: tuple
|
|
||||||
|
|
||||||
:param kwargs: Dictionary of injections.
|
:param kwargs: Dictionary of injections.
|
||||||
:type kwargs: dict
|
:type kwargs: dict
|
||||||
"""
|
|
||||||
self.attributes += tuple(injection
|
|
||||||
for injection in args
|
|
||||||
if is_attribute_injection(injection))
|
|
||||||
|
|
||||||
super(Factory, self).add_injections(*args, **kwargs)
|
:return: Reference ``self``
|
||||||
|
"""
|
||||||
|
self._attributes += _parse_attribute_injections(kwargs)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def _provide(self, *args, **kwargs):
|
def _provide(self, *args, **kwargs):
|
||||||
|
@ -140,16 +133,16 @@ class Factory(Callable):
|
||||||
|
|
||||||
:rtype: object
|
:rtype: object
|
||||||
"""
|
"""
|
||||||
if self.args:
|
if self._args:
|
||||||
args = tuple(arg.value for arg in self.args) + args
|
args = tuple(arg.value for arg in self._args) + args
|
||||||
|
|
||||||
for kwarg in self.kwargs:
|
for kwarg in self._kwargs:
|
||||||
if kwarg.name not in kwargs:
|
if kwarg.name not in kwargs:
|
||||||
kwargs[kwarg.name] = kwarg.value
|
kwargs[kwarg.name] = kwarg.value
|
||||||
|
|
||||||
instance = self.provides(*args, **kwargs)
|
instance = self.provides(*args, **kwargs)
|
||||||
|
|
||||||
for attribute in self.attributes:
|
for attribute in self._attributes:
|
||||||
setattr(instance, attribute.name, attribute.value)
|
setattr(instance, attribute.name, attribute.value)
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
|
|
|
@ -382,7 +382,8 @@ class CopyingTests(unittest.TestCase):
|
||||||
"""Test catalog providers copying."""
|
"""Test catalog providers copying."""
|
||||||
class CatalogA(catalogs.DeclarativeCatalog):
|
class CatalogA(catalogs.DeclarativeCatalog):
|
||||||
p11 = providers.Object(0)
|
p11 = providers.Object(0)
|
||||||
p12 = providers.Factory(dict, p11=p11)
|
p12 = providers.Factory(dict) \
|
||||||
|
.kwargs(p11=p11)
|
||||||
|
|
||||||
@catalogs.copy(CatalogA)
|
@catalogs.copy(CatalogA)
|
||||||
class CatalogA1(CatalogA):
|
class CatalogA1(CatalogA):
|
||||||
|
|
|
@ -4,7 +4,6 @@ import unittest2 as unittest
|
||||||
|
|
||||||
from dependency_injector import (
|
from dependency_injector import (
|
||||||
providers,
|
providers,
|
||||||
injections,
|
|
||||||
utils,
|
utils,
|
||||||
errors,
|
errors,
|
||||||
)
|
)
|
||||||
|
@ -35,7 +34,9 @@ class CallableTests(unittest.TestCase):
|
||||||
|
|
||||||
New simplified syntax.
|
New simplified syntax.
|
||||||
"""
|
"""
|
||||||
provider = providers.Callable(self.example, 1, 2, 3, 4)
|
provider = providers.Callable(self.example) \
|
||||||
|
.args(1, 2, 3, 4)
|
||||||
|
|
||||||
self.assertTupleEqual(provider(), (1, 2, 3, 4))
|
self.assertTupleEqual(provider(), (1, 2, 3, 4))
|
||||||
|
|
||||||
def test_call_with_keyword_args(self):
|
def test_call_with_keyword_args(self):
|
||||||
|
@ -43,11 +44,9 @@ class CallableTests(unittest.TestCase):
|
||||||
|
|
||||||
New simplified syntax.
|
New simplified syntax.
|
||||||
"""
|
"""
|
||||||
provider = providers.Callable(self.example,
|
provider = providers.Callable(self.example) \
|
||||||
arg1=1,
|
.kwargs(arg1=1, arg2=2, arg3=3, arg4=4)
|
||||||
arg2=2,
|
|
||||||
arg3=3,
|
|
||||||
arg4=4)
|
|
||||||
self.assertTupleEqual(provider(), (1, 2, 3, 4))
|
self.assertTupleEqual(provider(), (1, 2, 3, 4))
|
||||||
|
|
||||||
def test_call_with_positional_and_keyword_args(self):
|
def test_call_with_positional_and_keyword_args(self):
|
||||||
|
@ -55,40 +54,37 @@ class CallableTests(unittest.TestCase):
|
||||||
|
|
||||||
Simplified syntax of positional and keyword arg injections.
|
Simplified syntax of positional and keyword arg injections.
|
||||||
"""
|
"""
|
||||||
provider = providers.Callable(self.example, 1, 2, arg3=3, arg4=4)
|
provider = providers.Callable(self.example) \
|
||||||
self.assertTupleEqual(provider(), (1, 2, 3, 4))
|
.args(1, 2) \
|
||||||
|
.kwargs(arg3=3, arg4=4)
|
||||||
|
|
||||||
def test_call_with_positional_and_keyword_args_extended_syntax(self):
|
|
||||||
"""Test call with positional and keyword args.
|
|
||||||
|
|
||||||
Extended syntax of positional and keyword arg injections.
|
|
||||||
"""
|
|
||||||
provider = providers.Callable(self.example,
|
|
||||||
injections.Arg(1),
|
|
||||||
injections.Arg(2),
|
|
||||||
injections.KwArg('arg3', 3),
|
|
||||||
injections.KwArg('arg4', 4))
|
|
||||||
self.assertTupleEqual(provider(), (1, 2, 3, 4))
|
self.assertTupleEqual(provider(), (1, 2, 3, 4))
|
||||||
|
|
||||||
def test_call_with_context_args(self):
|
def test_call_with_context_args(self):
|
||||||
"""Test call with context args."""
|
"""Test call with context args."""
|
||||||
provider = providers.Callable(self.example, 1, 2)
|
provider = providers.Callable(self.example) \
|
||||||
|
.args(1, 2)
|
||||||
|
|
||||||
self.assertTupleEqual(provider(3, 4), (1, 2, 3, 4))
|
self.assertTupleEqual(provider(3, 4), (1, 2, 3, 4))
|
||||||
|
|
||||||
def test_call_with_context_kwargs(self):
|
def test_call_with_context_kwargs(self):
|
||||||
"""Test call with context kwargs."""
|
"""Test call with context kwargs."""
|
||||||
provider = providers.Callable(self.example,
|
provider = providers.Callable(self.example) \
|
||||||
injections.KwArg('arg1', 1))
|
.kwargs(arg1=1)
|
||||||
|
|
||||||
self.assertTupleEqual(provider(arg2=2, arg3=3, arg4=4), (1, 2, 3, 4))
|
self.assertTupleEqual(provider(arg2=2, arg3=3, arg4=4), (1, 2, 3, 4))
|
||||||
|
|
||||||
def test_call_with_context_args_and_kwargs(self):
|
def test_call_with_context_args_and_kwargs(self):
|
||||||
"""Test call with context args and kwargs."""
|
"""Test call with context args and kwargs."""
|
||||||
provider = providers.Callable(self.example, 1)
|
provider = providers.Callable(self.example) \
|
||||||
|
.args(1)
|
||||||
|
|
||||||
self.assertTupleEqual(provider(2, arg3=3, arg4=4), (1, 2, 3, 4))
|
self.assertTupleEqual(provider(2, arg3=3, arg4=4), (1, 2, 3, 4))
|
||||||
|
|
||||||
def test_call_overridden(self):
|
def test_call_overridden(self):
|
||||||
"""Test creation of new instances on overridden provider."""
|
"""Test creation of new instances on overridden provider."""
|
||||||
provider = providers.Callable(self.example)
|
provider = providers.Callable(self.example)
|
||||||
|
|
||||||
provider.override(providers.Object((4, 3, 2, 1)))
|
provider.override(providers.Object((4, 3, 2, 1)))
|
||||||
provider.override(providers.Object((1, 2, 3, 4)))
|
provider.override(providers.Object((1, 2, 3, 4)))
|
||||||
|
|
||||||
|
@ -96,24 +92,20 @@ class CallableTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_injections(self):
|
def test_injections(self):
|
||||||
"""Test getting a full list of injections using injections property."""
|
"""Test getting a full list of injections using injections property."""
|
||||||
provider = providers.Callable(self.example, 1, 2, arg3=3, arg4=4)
|
provider = providers.Callable(self.example) \
|
||||||
|
.args(1, 2) \
|
||||||
|
.kwargs(arg3=3, arg4=4)
|
||||||
|
|
||||||
self.assertEquals(len(provider.injections), 4)
|
self.assertEquals(len(provider.injections), 4)
|
||||||
|
|
||||||
def test_repr(self):
|
def test_repr(self):
|
||||||
"""Test representation of provider."""
|
"""Test representation of provider."""
|
||||||
provider = providers.Callable(self.example,
|
provider = providers.Callable(self.example) \
|
||||||
injections.KwArg(
|
.kwargs(arg1=providers.Factory(dict),
|
||||||
'arg1',
|
arg2=providers.Factory(list),
|
||||||
providers.Factory(dict)),
|
arg3=providers.Factory(set),
|
||||||
injections.KwArg(
|
arg4=providers.Factory(tuple))
|
||||||
'arg2',
|
|
||||||
providers.Factory(list)),
|
|
||||||
injections.KwArg(
|
|
||||||
'arg3',
|
|
||||||
providers.Factory(set)),
|
|
||||||
injections.KwArg(
|
|
||||||
'arg4',
|
|
||||||
providers.Factory(tuple)))
|
|
||||||
self.assertEqual(repr(provider),
|
self.assertEqual(repr(provider),
|
||||||
'<dependency_injector.providers.callable.'
|
'<dependency_injector.providers.callable.'
|
||||||
'Callable({0}) at {1}>'.format(
|
'Callable({0}) at {1}>'.format(
|
||||||
|
|
|
@ -47,7 +47,8 @@ class FactoryTests(unittest.TestCase):
|
||||||
|
|
||||||
provided_type = Example
|
provided_type = Example
|
||||||
|
|
||||||
example_provider = ExampleProvider(Example, 1, 2)
|
example_provider = ExampleProvider(Example) \
|
||||||
|
.args(1, 2)
|
||||||
|
|
||||||
self.assertIsInstance(example_provider(), Example)
|
self.assertIsInstance(example_provider(), Example)
|
||||||
|
|
||||||
|
@ -61,7 +62,8 @@ class FactoryTests(unittest.TestCase):
|
||||||
class NewExampe(Example):
|
class NewExampe(Example):
|
||||||
"""Example class subclass."""
|
"""Example class subclass."""
|
||||||
|
|
||||||
example_provider = ExampleProvider(NewExampe, 1, 2)
|
example_provider = ExampleProvider(NewExampe) \
|
||||||
|
.args(1, 2)
|
||||||
|
|
||||||
self.assertIsInstance(example_provider(), NewExampe)
|
self.assertIsInstance(example_provider(), NewExampe)
|
||||||
|
|
||||||
|
@ -90,7 +92,8 @@ class FactoryTests(unittest.TestCase):
|
||||||
|
|
||||||
New simplified syntax.
|
New simplified syntax.
|
||||||
"""
|
"""
|
||||||
provider = providers.Factory(Example, 'i1', 'i2')
|
provider = providers.Factory(Example) \
|
||||||
|
.args('i1', 'i2')
|
||||||
|
|
||||||
instance1 = provider()
|
instance1 = provider()
|
||||||
instance2 = provider()
|
instance2 = provider()
|
||||||
|
@ -110,7 +113,8 @@ class FactoryTests(unittest.TestCase):
|
||||||
|
|
||||||
New simplified syntax.
|
New simplified syntax.
|
||||||
"""
|
"""
|
||||||
provider = providers.Factory(Example, init_arg1='i1', init_arg2='i2')
|
provider = providers.Factory(Example) \
|
||||||
|
.kwargs(init_arg1='i1', init_arg2='i2')
|
||||||
|
|
||||||
instance1 = provider()
|
instance1 = provider()
|
||||||
instance2 = provider()
|
instance2 = provider()
|
||||||
|
@ -130,7 +134,9 @@ class FactoryTests(unittest.TestCase):
|
||||||
|
|
||||||
Simplified syntax of positional and keyword arg injections.
|
Simplified syntax of positional and keyword arg injections.
|
||||||
"""
|
"""
|
||||||
provider = providers.Factory(Example, 'i1', init_arg2='i2')
|
provider = providers.Factory(Example) \
|
||||||
|
.args('i1') \
|
||||||
|
.kwargs(init_arg2='i2')
|
||||||
|
|
||||||
instance1 = provider()
|
instance1 = provider()
|
||||||
instance2 = provider()
|
instance2 = provider()
|
||||||
|
@ -150,9 +156,9 @@ class FactoryTests(unittest.TestCase):
|
||||||
|
|
||||||
Extended syntax of positional and keyword arg injections.
|
Extended syntax of positional and keyword arg injections.
|
||||||
"""
|
"""
|
||||||
provider = providers.Factory(Example,
|
provider = providers.Factory(Example) \
|
||||||
injections.Arg('i1'),
|
.args('i1') \
|
||||||
injections.KwArg('init_arg2', 'i2'))
|
.kwargs(init_arg2='i2')
|
||||||
|
|
||||||
instance1 = provider()
|
instance1 = provider()
|
||||||
instance2 = provider()
|
instance2 = provider()
|
||||||
|
@ -169,9 +175,8 @@ class FactoryTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_call_with_attributes(self):
|
def test_call_with_attributes(self):
|
||||||
"""Test creation of new instances with attribute injections."""
|
"""Test creation of new instances with attribute injections."""
|
||||||
provider = providers.Factory(Example,
|
provider = providers.Factory(Example) \
|
||||||
injections.Attribute('attribute1', 'a1'),
|
.attributes(attribute1='a1', attribute2='a2')
|
||||||
injections.Attribute('attribute2', 'a2'))
|
|
||||||
|
|
||||||
instance1 = provider()
|
instance1 = provider()
|
||||||
instance2 = provider()
|
instance2 = provider()
|
||||||
|
@ -188,7 +193,8 @@ class FactoryTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_call_with_context_args(self):
|
def test_call_with_context_args(self):
|
||||||
"""Test creation of new instances with context args."""
|
"""Test creation of new instances with context args."""
|
||||||
provider = providers.Factory(Example, 11, 22)
|
provider = providers.Factory(Example) \
|
||||||
|
.args(11, 22)
|
||||||
instance = provider(33, 44)
|
instance = provider(33, 44)
|
||||||
|
|
||||||
self.assertEqual(instance.init_arg1, 11)
|
self.assertEqual(instance.init_arg1, 11)
|
||||||
|
@ -198,8 +204,8 @@ class FactoryTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_call_with_context_kwargs(self):
|
def test_call_with_context_kwargs(self):
|
||||||
"""Test creation of new instances with context kwargs."""
|
"""Test creation of new instances with context kwargs."""
|
||||||
provider = providers.Factory(Example,
|
provider = providers.Factory(Example) \
|
||||||
injections.KwArg('init_arg1', 1))
|
.kwargs(init_arg1=1)
|
||||||
|
|
||||||
instance1 = provider(init_arg2=22)
|
instance1 = provider(init_arg2=22)
|
||||||
self.assertEqual(instance1.init_arg1, 1)
|
self.assertEqual(instance1.init_arg1, 1)
|
||||||
|
@ -211,7 +217,8 @@ class FactoryTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_call_with_context_args_and_kwargs(self):
|
def test_call_with_context_args_and_kwargs(self):
|
||||||
"""Test creation of new instances with context args and kwargs."""
|
"""Test creation of new instances with context args and kwargs."""
|
||||||
provider = providers.Factory(Example, 11)
|
provider = providers.Factory(Example) \
|
||||||
|
.args(11)
|
||||||
instance = provider(22, init_arg3=33, init_arg4=44)
|
instance = provider(22, init_arg3=33, init_arg4=44)
|
||||||
|
|
||||||
self.assertEqual(instance.init_arg1, 11)
|
self.assertEqual(instance.init_arg1, 11)
|
||||||
|
@ -237,20 +244,19 @@ class FactoryTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_injections(self):
|
def test_injections(self):
|
||||||
"""Test getting a full list of injections using injections property."""
|
"""Test getting a full list of injections using injections property."""
|
||||||
provider = providers.Factory(Example,
|
provider = providers.Factory(Example) \
|
||||||
injections.Arg(1),
|
.args(1) \
|
||||||
injections.KwArg('init_arg2', 2),
|
.kwargs(init_arg2=2) \
|
||||||
injections.Attribute('attribute1', 3),
|
.attributes(attribute1=3, attribute2=4)
|
||||||
injections.Attribute('attribute2', 4))
|
|
||||||
self.assertEquals(len(provider.injections), 4)
|
self.assertEquals(len(provider.injections), 4)
|
||||||
|
|
||||||
def test_repr(self):
|
def test_repr(self):
|
||||||
"""Test representation of provider."""
|
"""Test representation of provider."""
|
||||||
provider = providers.Factory(Example,
|
provider = providers.Factory(Example) \
|
||||||
injections.KwArg('init_arg1',
|
.kwargs(init_arg1=providers.Factory(dict),
|
||||||
providers.Factory(dict)),
|
init_arg2=providers.Factory(list))
|
||||||
injections.KwArg('init_arg2',
|
|
||||||
providers.Factory(list)))
|
|
||||||
self.assertEqual(repr(provider),
|
self.assertEqual(repr(provider),
|
||||||
'<dependency_injector.providers.creational.'
|
'<dependency_injector.providers.creational.'
|
||||||
'Factory({0}) at {1}>'.format(
|
'Factory({0}) at {1}>'.format(
|
||||||
|
@ -300,7 +306,8 @@ class SingletonTests(unittest.TestCase):
|
||||||
|
|
||||||
provided_type = Example
|
provided_type = Example
|
||||||
|
|
||||||
example_provider = ExampleProvider(Example, 1, 2)
|
example_provider = ExampleProvider(Example) \
|
||||||
|
.args(1, 2)
|
||||||
|
|
||||||
self.assertIsInstance(example_provider(), Example)
|
self.assertIsInstance(example_provider(), Example)
|
||||||
|
|
||||||
|
@ -314,7 +321,8 @@ class SingletonTests(unittest.TestCase):
|
||||||
class NewExampe(Example):
|
class NewExampe(Example):
|
||||||
"""Example class subclass."""
|
"""Example class subclass."""
|
||||||
|
|
||||||
example_provider = ExampleProvider(NewExampe, 1, 2)
|
example_provider = ExampleProvider(NewExampe) \
|
||||||
|
.args(1, 2)
|
||||||
|
|
||||||
self.assertIsInstance(example_provider(), NewExampe)
|
self.assertIsInstance(example_provider(), NewExampe)
|
||||||
|
|
||||||
|
@ -343,7 +351,8 @@ class SingletonTests(unittest.TestCase):
|
||||||
|
|
||||||
New simplified syntax.
|
New simplified syntax.
|
||||||
"""
|
"""
|
||||||
provider = providers.Singleton(Example, 'i1', 'i2')
|
provider = providers.Singleton(Example) \
|
||||||
|
.args('i1', 'i2')
|
||||||
|
|
||||||
instance1 = provider()
|
instance1 = provider()
|
||||||
instance2 = provider()
|
instance2 = provider()
|
||||||
|
@ -363,7 +372,8 @@ class SingletonTests(unittest.TestCase):
|
||||||
|
|
||||||
New simplified syntax.
|
New simplified syntax.
|
||||||
"""
|
"""
|
||||||
provider = providers.Singleton(Example, init_arg1='i1', init_arg2='i2')
|
provider = providers.Singleton(Example) \
|
||||||
|
.kwargs(init_arg1='i1', init_arg2='i2')
|
||||||
|
|
||||||
instance1 = provider()
|
instance1 = provider()
|
||||||
instance2 = provider()
|
instance2 = provider()
|
||||||
|
@ -383,7 +393,9 @@ class SingletonTests(unittest.TestCase):
|
||||||
|
|
||||||
Simplified syntax of positional and keyword arg injections.
|
Simplified syntax of positional and keyword arg injections.
|
||||||
"""
|
"""
|
||||||
provider = providers.Singleton(Example, 'i1', init_arg2='i2')
|
provider = providers.Singleton(Example) \
|
||||||
|
.args('i1') \
|
||||||
|
.kwargs(init_arg2='i2')
|
||||||
|
|
||||||
instance1 = provider()
|
instance1 = provider()
|
||||||
instance2 = provider()
|
instance2 = provider()
|
||||||
|
@ -403,9 +415,9 @@ class SingletonTests(unittest.TestCase):
|
||||||
|
|
||||||
Extended syntax of positional and keyword arg injections.
|
Extended syntax of positional and keyword arg injections.
|
||||||
"""
|
"""
|
||||||
provider = providers.Singleton(Example,
|
provider = providers.Singleton(Example) \
|
||||||
injections.Arg('i1'),
|
.args('i1') \
|
||||||
injections.KwArg('init_arg2', 'i2'))
|
.kwargs(init_arg2='i2')
|
||||||
|
|
||||||
instance1 = provider()
|
instance1 = provider()
|
||||||
instance2 = provider()
|
instance2 = provider()
|
||||||
|
@ -422,11 +434,8 @@ class SingletonTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_call_with_attributes(self):
|
def test_call_with_attributes(self):
|
||||||
"""Test getting of instances with attribute injections."""
|
"""Test getting of instances with attribute injections."""
|
||||||
provider = providers.Singleton(Example,
|
provider = providers.Singleton(Example) \
|
||||||
injections.Attribute('attribute1',
|
.attributes(attribute1='a1', attribute2='a2')
|
||||||
'a1'),
|
|
||||||
injections.Attribute('attribute2',
|
|
||||||
'a2'))
|
|
||||||
|
|
||||||
instance1 = provider()
|
instance1 = provider()
|
||||||
instance2 = provider()
|
instance2 = provider()
|
||||||
|
@ -451,8 +460,8 @@ class SingletonTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_call_with_context_kwargs(self):
|
def test_call_with_context_kwargs(self):
|
||||||
"""Test getting of instances with context kwargs."""
|
"""Test getting of instances with context kwargs."""
|
||||||
provider = providers.Singleton(Example,
|
provider = providers.Singleton(Example) \
|
||||||
injections.KwArg('init_arg1', 1))
|
.kwargs(init_arg1=1)
|
||||||
|
|
||||||
instance1 = provider(init_arg2=22)
|
instance1 = provider(init_arg2=22)
|
||||||
self.assertEqual(instance1.init_arg1, 1)
|
self.assertEqual(instance1.init_arg1, 1)
|
||||||
|
@ -465,7 +474,8 @@ class SingletonTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_call_with_context_args_and_kwargs(self):
|
def test_call_with_context_args_and_kwargs(self):
|
||||||
"""Test getting of instances with context args and kwargs."""
|
"""Test getting of instances with context args and kwargs."""
|
||||||
provider = providers.Singleton(Example, 11)
|
provider = providers.Singleton(Example) \
|
||||||
|
.args(11)
|
||||||
instance = provider(22, init_arg3=33, init_arg4=44)
|
instance = provider(22, init_arg3=33, init_arg4=44)
|
||||||
|
|
||||||
self.assertEqual(instance.init_arg1, 11)
|
self.assertEqual(instance.init_arg1, 11)
|
||||||
|
@ -496,28 +506,32 @@ class SingletonTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_args_attr(self):
|
def test_args_attr(self):
|
||||||
"""Test args attribute."""
|
"""Test args attribute."""
|
||||||
provider = providers.Singleton(Example, 1, 2)
|
provider = providers.Singleton(Example) \
|
||||||
self.assertEquals(len(provider.args), 2)
|
.args(1, 2)
|
||||||
|
|
||||||
|
self.assertEquals(len(provider._args), 2)
|
||||||
|
|
||||||
def test_kwargs_attr(self):
|
def test_kwargs_attr(self):
|
||||||
"""Test kwargs attribute."""
|
"""Test kwargs attribute."""
|
||||||
provider = providers.Singleton(Example, init_arg1=1, init_arg2=2)
|
provider = providers.Singleton(Example) \
|
||||||
self.assertEquals(len(provider.kwargs), 2)
|
.kwargs(init_arg1=1, init_arg2=2)
|
||||||
|
|
||||||
|
self.assertEquals(len(provider._kwargs), 2)
|
||||||
|
|
||||||
def test_attributes_attr(self):
|
def test_attributes_attr(self):
|
||||||
"""Test attributes attribute."""
|
"""Test attributes attribute."""
|
||||||
provider = providers.Singleton(Example,
|
provider = providers.Singleton(Example) \
|
||||||
injections.Attribute('attribute1', 1),
|
.attributes(attribute1=1, attribute2=2)
|
||||||
injections.Attribute('attribute2', 2))
|
|
||||||
self.assertEquals(len(provider.attributes), 2)
|
self.assertEquals(len(provider._attributes), 2)
|
||||||
|
|
||||||
def test_injections(self):
|
def test_injections(self):
|
||||||
"""Test getting a full list of injections using injections property."""
|
"""Test getting a full list of injections using injections property."""
|
||||||
provider = providers.Singleton(Example,
|
provider = providers.Singleton(Example) \
|
||||||
injections.Arg(1),
|
.args(1) \
|
||||||
injections.KwArg('init_arg2', 2),
|
.kwargs(init_arg2=2) \
|
||||||
injections.Attribute('attribute1', 3),
|
.attributes(attribute1=3, attribute2=4)
|
||||||
injections.Attribute('attribute2', 4))
|
|
||||||
self.assertEquals(len(provider.injections), 4)
|
self.assertEquals(len(provider.injections), 4)
|
||||||
|
|
||||||
def test_reset(self):
|
def test_reset(self):
|
||||||
|
@ -536,13 +550,10 @@ class SingletonTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_repr(self):
|
def test_repr(self):
|
||||||
"""Test representation of provider."""
|
"""Test representation of provider."""
|
||||||
provider = providers.Singleton(Example,
|
provider = providers.Singleton(Example) \
|
||||||
injections.KwArg(
|
.kwargs(init_arg1=providers.Factory(dict),
|
||||||
'init_arg1',
|
init_arg2=providers.Factory(list))
|
||||||
providers.Factory(dict)),
|
|
||||||
injections.KwArg(
|
|
||||||
'init_arg2',
|
|
||||||
providers.Factory(list)))
|
|
||||||
self.assertEqual(repr(provider),
|
self.assertEqual(repr(provider),
|
||||||
'<dependency_injector.providers.creational.'
|
'<dependency_injector.providers.creational.'
|
||||||
'Singleton({0}) at {1}>'.format(
|
'Singleton({0}) at {1}>'.format(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user