Drop method injections

This commit is contained in:
Roman Mogilatov 2016-05-17 22:37:50 +03:00
parent 4a160ed999
commit 2878ea5515
7 changed files with 9 additions and 173 deletions

View File

@ -91,7 +91,7 @@ class _NamedInjection(Injection):
.. py:attribute:: name
Injection target's name (keyword argument, attribute, method).
Injection target's name (keyword argument, attribute).
:type: str
"""
@ -153,19 +153,6 @@ class Attribute(_NamedInjection):
__IS_ATTRIBUTE_INJECTION__ = True
class Method(_NamedInjection):
"""Method injection.
.. py:attribute:: name
Method's name.
:type: str
"""
__IS_METHOD_INJECTION__ = True
def inject(*args, **kwargs):
"""Dependency injection decorator.

View File

@ -3,7 +3,6 @@
from dependency_injector.providers.callable import Callable
from dependency_injector.utils import (
is_attribute_injection,
is_method_injection,
GLOBAL_LOCK,
)
from dependency_injector.errors import Error
@ -76,17 +75,11 @@ class Factory(Callable):
Tuple of attribute injections.
:type: tuple[:py:class:`dependency_injector.injections.Attribute`]
.. py:attribute:: methods
Tuple of method injections.
:type: tuple[:py:class:`dependency_injector.injections.Method`]
"""
provided_type = None
__slots__ = ('cls', 'attributes', 'methods')
__slots__ = ('cls', 'attributes')
def __init__(self, provides, *args, **kwargs):
"""Initializer.
@ -107,7 +100,6 @@ class Factory(Callable):
self.__class__, self.__class__.provided_type))
self.attributes = tuple()
self.methods = tuple()
super(Factory, self).__init__(provides, *args, **kwargs)
@ -119,7 +111,7 @@ class Factory(Callable):
:rtype: tuple[:py:class:`dependency_injector.injections.Injection`]
"""
return self.args + self.kwargs + self.attributes + self.methods
return self.args + self.kwargs + self.attributes
def add_injections(self, *args, **kwargs):
"""Add provider injections.
@ -134,11 +126,8 @@ class Factory(Callable):
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)
return self
def _provide(self, *args, **kwargs):
"""Return provided instance.
@ -162,8 +151,6 @@ class Factory(Callable):
for attribute in self.attributes:
setattr(instance, attribute.name, attribute.value)
for method in self.methods:
getattr(instance, method.name)(method.value)
return instance
@ -212,12 +199,6 @@ class DelegatedFactory(Factory):
Tuple of attribute injections.
:type: tuple[:py:class:`dependency_injector.injections.Attribute`]
.. py:attribute:: methods
Tuple of method injections.
:type: tuple[:py:class:`dependency_injector.injections.Method`]
"""
__IS_DELEGATED__ = True
@ -287,12 +268,6 @@ class Singleton(Factory):
Tuple of attribute injections.
:type: tuple[:py:class:`dependency_injector.injections.Attribute`]
.. py:attribute:: methods
Tuple of method injections.
:type: tuple[:py:class:`dependency_injector.injections.Method`]
"""
__slots__ = ('instance',)
@ -390,12 +365,6 @@ class DelegatedSingleton(Singleton):
Tuple of attribute injections.
:type: tuple[:py:class:`dependency_injector.injections.Attribute`]
.. py:attribute:: methods
Tuple of method injections.
:type: tuple[:py:class:`dependency_injector.injections.Method`]
"""
__IS_DELEGATED__ = True

View File

@ -141,19 +141,6 @@ def is_attribute_injection(instance):
getattr(instance, '__IS_ATTRIBUTE_INJECTION__', False) is True)
def is_method_injection(instance):
"""Check if instance is method injection instance.
:param instance: Instance to be checked.
:type instance: object
:rtype: bool
"""
return (not isinstance(instance, six.class_types) and
hasattr(instance, '__IS_METHOD_INJECTION__') and
getattr(instance, '__IS_METHOD_INJECTION__', False) is True)
def is_catalog(instance):
"""Check if instance is catalog instance.

View File

@ -20,6 +20,7 @@ Development version
- ``Function``
- ``Class``
- ``Config``
- Drop ``Method`` injections.
1.17.0
------

View File

@ -24,17 +24,6 @@ class Example(object):
self.attribute1 = None
self.attribute2 = None
self.method1_value = None
self.method2_value = None
def method1(self, value):
"""Setter method 1."""
self.method1_value = value
def method2(self, value):
"""Setter method 2."""
self.method2_value = value
class FactoryTests(unittest.TestCase):
"""Factory test cases."""
@ -197,25 +186,6 @@ class FactoryTests(unittest.TestCase):
self.assertIsInstance(instance1, Example)
self.assertIsInstance(instance2, Example)
def test_call_with_methods(self):
"""Test creation of new instances with method injections."""
provider = providers.Factory(Example,
injections.Method('method1', 'm1'),
injections.Method('method2', 'm2'))
instance1 = provider()
instance2 = provider()
self.assertEqual(instance1.method1_value, 'm1')
self.assertEqual(instance1.method2_value, 'm2')
self.assertEqual(instance2.method1_value, 'm1')
self.assertEqual(instance2.method2_value, 'm2')
self.assertIsNot(instance1, instance2)
self.assertIsInstance(instance1, Example)
self.assertIsInstance(instance2, Example)
def test_call_with_context_args(self):
"""Test creation of new instances with context args."""
provider = providers.Factory(Example, 11, 22)
@ -271,10 +241,8 @@ class FactoryTests(unittest.TestCase):
injections.Arg(1),
injections.KwArg('init_arg2', 2),
injections.Attribute('attribute1', 3),
injections.Attribute('attribute2', 4),
injections.Method('method1', 5),
injections.Method('method2', 6))
self.assertEquals(len(provider.injections), 6)
injections.Attribute('attribute2', 4))
self.assertEquals(len(provider.injections), 4)
def test_repr(self):
"""Test representation of provider."""
@ -473,25 +441,6 @@ class SingletonTests(unittest.TestCase):
self.assertIsInstance(instance1, Example)
self.assertIsInstance(instance2, Example)
def test_call_with_methods(self):
"""Test getting of instances with method injections."""
provider = providers.Singleton(Example,
injections.Method('method1', 'm1'),
injections.Method('method2', 'm2'))
instance1 = provider()
instance2 = provider()
self.assertEqual(instance1.method1_value, 'm1')
self.assertEqual(instance1.method2_value, 'm2')
self.assertEqual(instance2.method1_value, 'm1')
self.assertEqual(instance2.method2_value, 'm2')
self.assertIs(instance1, instance2)
self.assertIsInstance(instance1, Example)
self.assertIsInstance(instance2, Example)
def test_call_with_context_args(self):
"""Test getting of instances with context args."""
provider = providers.Singleton(Example)
@ -562,23 +511,14 @@ class SingletonTests(unittest.TestCase):
injections.Attribute('attribute2', 2))
self.assertEquals(len(provider.attributes), 2)
def test_methods_attr(self):
"""Test methods attribute."""
provider = providers.Singleton(Example,
injections.Method('method1', 1),
injections.Method('method2', 2))
self.assertEquals(len(provider.methods), 2)
def test_injections(self):
"""Test getting a full list of injections using injections property."""
provider = providers.Singleton(Example,
injections.Arg(1),
injections.KwArg('init_arg2', 2),
injections.Attribute('attribute1', 3),
injections.Attribute('attribute2', 4),
injections.Method('method1', 5),
injections.Method('method2', 6))
self.assertEquals(len(provider.injections), 6)
injections.Attribute('attribute2', 4))
self.assertEquals(len(provider.injections), 4)
def test_reset(self):
"""Test creation and reset of single object."""

View File

@ -110,27 +110,6 @@ class AttributeTests(unittest.TestCase):
hex(id(injection))))
class MethodTests(unittest.TestCase):
"""Method injection test cases."""
def test_init(self):
"""Test Method creation and initialization."""
injection = injections.Method('some_arg_name', 'some_value')
self.assertEqual(injection.name, 'some_arg_name')
self.assertEqual(injection.injectable, 'some_value')
def test_repr(self):
"""Test Method representation."""
provider = providers.Factory(object)
injection = injections.Method('name', provider)
self.assertEqual(
repr(injection),
'<dependency_injector.injections.Method({0}, {1}) at {2}>'.format(
repr('name'),
repr(provider),
hex(id(injection))))
class InjectTests(unittest.TestCase):
"""Inject decorator test cases."""

View File

@ -86,8 +86,6 @@ class IsInjectionTests(unittest.TestCase):
'value')))
self.assertTrue(utils.is_injection(injections.Attribute('name',
'value')))
self.assertTrue(utils.is_injection(injections.Method('name',
'value')))
def test_with_class(self):
"""Test with class."""
@ -203,31 +201,6 @@ class IsAttributeInjectionTests(unittest.TestCase):
self.assertFalse(utils.is_attribute_injection(object()))
class IsMethodInjectionTests(unittest.TestCase):
"""`is_method_injection()` test cases."""
def test_with_instance(self):
"""Test with instance."""
self.assertTrue(utils.is_method_injection(
injections.Method('name', 'value')))
def test_with_class(self):
"""Test with class."""
self.assertFalse(utils.is_method_injection(injections.Method))
def test_with_parent_class(self):
"""Test with parent class."""
self.assertFalse(utils.is_method_injection(injections.Injection))
def test_with_string(self):
"""Test with string."""
self.assertFalse(utils.is_method_injection('some_string'))
def test_with_object(self):
"""Test with object."""
self.assertFalse(utils.is_method_injection(object()))
class IsCatalogTests(unittest.TestCase):
"""`is_catalog()` test cases."""