Drop Static, Value, Function & Class providers

This commit is contained in:
Roman Mogilatov 2016-05-17 21:28:22 +03:00
parent 7392f35991
commit 1320c12780
9 changed files with 41 additions and 151 deletions

View File

@ -3,7 +3,7 @@
from dependency_injector.providers.base import (
Provider,
Delegate,
Static,
Object,
ExternalDependency,
)
from dependency_injector.providers.callable import (
@ -16,12 +16,6 @@ from dependency_injector.providers.creational import (
Singleton,
DelegatedSingleton,
)
from dependency_injector.providers.static import (
Object,
Value,
Class,
Function,
)
from dependency_injector.providers.config import (
Config,
ChildConfig,
@ -35,7 +29,7 @@ from dependency_injector.providers.utils import (
__all__ = (
'Provider',
'Delegate',
'Static', 'StaticProvider',
'Object',
'ExternalDependency',
'Callable',
@ -46,11 +40,6 @@ __all__ = (
'Singleton',
'DelegatedSingleton',
'Object',
'Value',
'Class',
'Function',
'Config',
'ChildConfig',

View File

@ -116,7 +116,7 @@ class Provider(object):
'with itself'.format(self))
if not is_provider(provider):
provider = Static(provider)
provider = Object(provider)
if not self.is_overridden:
self.overridden_by = (ensure_is_provider(provider),)
@ -229,11 +229,8 @@ class Delegate(Provider):
@six.python_2_unicode_compatible
class Static(Provider):
""":py:class:`Static` provider returns provided instance "as is".
:py:class:`Static` provider is base implementation that provides exactly
the same as it got on input.
class Object(Provider):
""":py:class:`Object` provider returns provided instance "as is".
.. py:attribute:: provides
@ -251,7 +248,7 @@ class Static(Provider):
:type provides: object
"""
self.provides = provides
super(Static, self).__init__()
super(Object, self).__init__()
def _provide(self, *args, **kwargs):
"""Return provided instance.

View File

@ -1,43 +0,0 @@
"""Dependency injector static providers."""
from dependency_injector.providers.base import Static
class Class(Static):
""":py:class:`Class` returns provided class "as is".
.. code-block:: python
cls_provider = Class(object)
object_cls = cls_provider()
"""
class Object(Static):
""":py:class:`Object` returns provided object "as is".
.. code-block:: python
object_provider = Object(object())
object_instance = object_provider()
"""
class Function(Static):
""":py:class:`Function` returns provided function "as is".
.. code-block:: python
function_provider = Function(len)
len_function = function_provider()
"""
class Value(Static):
""":py:class:`Value` returns provided value "as is".
.. code-block:: python
value_provider = Value(31337)
value = value_provider()
"""

View File

@ -14,6 +14,7 @@ Development version
2.0.0
------
- Drop backward compatibilities of 1.x.
- Drop ``Static``, ``Value``, ``Function`` & ``Class`` providers.
1.17.0
------

View File

@ -268,7 +268,7 @@ class DeclarativeCatalogTests(unittest.TestCase):
def test_filter_all_providers_by_type(self):
"""Test getting of all catalog providers of specific type."""
self.assertTrue(len(CatalogB.filter(providers.Provider)) == 4)
self.assertTrue(len(CatalogB.filter(providers.Value)) == 0)
self.assertTrue(len(CatalogB.filter(providers.Object)) == 0)
def test_repr(self):
"""Test catalog representation."""
@ -381,18 +381,18 @@ class CopyingTests(unittest.TestCase):
def test_copy_with_replacing(self):
"""Test catalog providers copying."""
class CatalogA(catalogs.DeclarativeCatalog):
p11 = providers.Value(0)
p11 = providers.Object(0)
p12 = providers.Factory(dict, p11=p11)
@catalogs.copy(CatalogA)
class CatalogA1(CatalogA):
p11 = providers.Value(1)
p13 = providers.Value(11)
p11 = providers.Object(1)
p13 = providers.Object(11)
@catalogs.copy(CatalogA)
class CatalogA2(CatalogA):
p11 = providers.Value(2)
p13 = providers.Value(22)
p11 = providers.Object(2)
p13 = providers.Object(22)
self.assertIsNot(CatalogA.p11, CatalogA1.p11)
self.assertIsNot(CatalogA.p12, CatalogA1.p12)

View File

@ -210,7 +210,7 @@ class DynamicCatalogTests(unittest.TestCase):
def test_filter_all_providers_by_type(self):
"""Test getting of all catalog providers of specific type."""
self.assertTrue(len(self.catalog.filter(providers.Provider)) == 2)
self.assertTrue(len(self.catalog.filter(providers.Value)) == 0)
self.assertTrue(len(self.catalog.filter(providers.Object)) == 0)
def test_repr(self):
"""Test catalog representation."""

View File

@ -36,8 +36,8 @@ class OverrideTests(unittest.TestCase):
class OverridingCatalog(catalogs.DeclarativeCatalog):
"""Overriding catalog."""
p11 = providers.Value(1)
p12 = providers.Value(2)
p11 = providers.Object(1)
p12 = providers.Object(2)
self.assertEqual(CatalogA.p11(), 1)
self.assertEqual(CatalogA.p12(), 2)
@ -55,15 +55,15 @@ class OverrideTests(unittest.TestCase):
def test_override_dynamic_catalog_with_itself(self):
"""Test catalog overriding of dynamic catalog with itself."""
catalog = catalogs.DynamicCatalog(p11=providers.Value(1),
p12=providers.Value(2))
catalog = catalogs.DynamicCatalog(p11=providers.Object(1),
p12=providers.Object(2))
with self.assertRaises(errors.Error):
catalog.override(catalog)
def test_overriding_with_dynamic_catalog(self):
"""Test catalog overriding with another dynamic catalog."""
CatalogA.override(catalogs.DynamicCatalog(p11=providers.Value(1),
p12=providers.Value(2)))
CatalogA.override(catalogs.DynamicCatalog(p11=providers.Object(1),
p12=providers.Object(2)))
self.assertEqual(CatalogA.p11(), 1)
self.assertEqual(CatalogA.p12(), 2)
self.assertEqual(len(CatalogA.overridden_by), 1)
@ -100,15 +100,15 @@ class OverrideTests(unittest.TestCase):
class OverridingCatalog1(catalogs.DeclarativeCatalog):
"""Overriding catalog."""
p11 = providers.Value(1)
p12 = providers.Value(2)
p11 = providers.Object(1)
p12 = providers.Object(2)
@catalogs.override(CatalogA)
class OverridingCatalog2(catalogs.DeclarativeCatalog):
"""Overriding catalog."""
p11 = providers.Value(3)
p12 = providers.Value(4)
p11 = providers.Object(3)
p12 = providers.Object(4)
CatalogA.reset_last_overriding()
@ -126,15 +126,15 @@ class OverrideTests(unittest.TestCase):
class OverridingCatalog1(catalogs.DeclarativeCatalog):
"""Overriding catalog."""
p11 = providers.Value(1)
p12 = providers.Value(2)
p11 = providers.Object(1)
p12 = providers.Object(2)
@catalogs.override(CatalogA)
class OverridingCatalog2(catalogs.DeclarativeCatalog):
"""Overriding catalog."""
p11 = providers.Value(3)
p12 = providers.Value(4)
p11 = providers.Object(3)
p12 = providers.Object(4)
CatalogA.reset_override()

View File

@ -89,8 +89,8 @@ class CallableTests(unittest.TestCase):
def test_call_overridden(self):
"""Test creation of new instances on overridden provider."""
provider = providers.Callable(self.example)
provider.override(providers.Value((4, 3, 2, 1)))
provider.override(providers.Value((1, 2, 3, 4)))
provider.override(providers.Object((4, 3, 2, 1)))
provider.override(providers.Object((1, 2, 3, 4)))
self.assertTupleEqual(provider(), (1, 2, 3, 4))

View File

@ -8,86 +8,32 @@ from dependency_injector import (
)
class StaticProvidersTests(unittest.TestCase):
"""Static providers test cases."""
class ObjectProviderTests(unittest.TestCase):
"""Object provider tests."""
def test_is_provider(self):
"""Test `is_provider` check."""
self.assertTrue(utils.is_provider(providers.Class(object)))
self.assertTrue(utils.is_provider(providers.Object(object())))
self.assertTrue(utils.is_provider(providers.Function(map)))
self.assertTrue(utils.is_provider(providers.Value(123)))
def test_call_class_provider(self):
"""Test Class provider call."""
self.assertIs(providers.Class(dict)(), dict)
def test_call_object_provider(self):
"""Test Object provider call."""
"""Test provider call."""
obj = object()
self.assertIs(providers.Object(obj)(), obj)
def test_call_function_provider(self):
"""Test Function provider call."""
self.assertIs(providers.Function(map)(), map)
def test_call_value_provider(self):
"""Test Value provider call."""
self.assertEqual(providers.Value(123)(), 123)
def test_call_overridden_class_provider(self):
"""Test overridden Class provider call."""
cls_provider = providers.Class(dict)
cls_provider.override(providers.Object(list))
self.assertIs(cls_provider(), list)
def test_call_overridden_object_provider(self):
"""Test overridden Object provider call."""
"""Test overridden provider call."""
obj1 = object()
obj2 = object()
obj_provider = providers.Object(obj1)
obj_provider.override(providers.Object(obj2))
self.assertIs(obj_provider(), obj2)
def test_call_overridden_function_provider(self):
"""Test overridden Function provider call."""
function_provider = providers.Function(len)
function_provider.override(providers.Function(sum))
self.assertIs(function_provider(), sum)
def test_call_overridden_value_provider(self):
"""Test overridden Value provider call."""
value_provider = providers.Value(123)
value_provider.override(providers.Value(321))
self.assertEqual(value_provider(), 321)
provider = providers.Object(obj1)
provider.override(providers.Object(obj2))
self.assertIs(provider(), obj2)
def test_repr(self):
"""Test representation of provider."""
class_provider = providers.Class(object)
self.assertEqual(repr(class_provider),
'<dependency_injector.providers.static.'
'Class({0}) at {1}>'.format(
repr(object),
hex(id(class_provider))))
some_object = object()
object_provider = providers.Object(some_object)
self.assertEqual(repr(object_provider),
'<dependency_injector.providers.static.'
provider = providers.Object(some_object)
self.assertEqual(repr(provider),
'<dependency_injector.providers.base.'
'Object({0}) at {1}>'.format(
repr(some_object),
hex(id(object_provider))))
function_provider = providers.Function(map)
self.assertEqual(repr(function_provider),
'<dependency_injector.providers.static.'
'Function({0}) at {1}>'.format(
repr(map),
hex(id(function_provider))))
value_provider = providers.Value(123)
self.assertEqual(repr(value_provider),
'<dependency_injector.providers.static.'
'Value({0}) at {1}>'.format(
repr(123),
hex(id(value_provider))))
hex(id(provider))))