mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-01-31 11:51:39 +03:00
Merge pull request #175 from ets-labs/feature/add-provider-attribute
Add ".provider" attribute for all providers
This commit is contained in:
commit
94a5bc21d2
|
@ -10,6 +10,8 @@ follows `Semantic versioning`_
|
||||||
Development version
|
Development version
|
||||||
-------------------
|
-------------------
|
||||||
- Add ``FactoryAggregate`` provider.
|
- Add ``FactoryAggregate`` provider.
|
||||||
|
- Add ``Provider.provider`` dynamic attribute that return new provider's
|
||||||
|
delegate (alias of method ``Provider.delegate()``).
|
||||||
- Add support of six 1.11.0.
|
- Add support of six 1.11.0.
|
||||||
- Regenerate C sources using Cython 0.27.1.
|
- Regenerate C sources using Cython 0.27.1.
|
||||||
|
|
||||||
|
|
|
@ -80,6 +80,9 @@ Actually, there are three ways for creating factory delegates:
|
||||||
:py:class:`Delegate` provider.
|
:py:class:`Delegate` provider.
|
||||||
+ ``Factory(...).delegate()`` - calling factory :py:meth:`Factory.delegate`
|
+ ``Factory(...).delegate()`` - calling factory :py:meth:`Factory.delegate`
|
||||||
method, that returns delegate wrapper for current factory.
|
method, that returns delegate wrapper for current factory.
|
||||||
|
+ ``Factory(...).provider`` - getting factory :py:attr:`Factory.provider`
|
||||||
|
attribute, that returns delegate wrapper for current factory (alias of
|
||||||
|
``Factory(...).delegate()`` method).
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
|
|
|
@ -51,3 +51,18 @@ user2 = users_factory() # Same as: user2 = User(photos_factory=photos_factory)
|
||||||
# Making some asserts:
|
# Making some asserts:
|
||||||
assert isinstance(user1.main_photo, Photo)
|
assert isinstance(user1.main_photo, Photo)
|
||||||
assert isinstance(user2.main_photo, Photo)
|
assert isinstance(user2.main_photo, Photo)
|
||||||
|
|
||||||
|
# or using Factory(...).provider
|
||||||
|
|
||||||
|
photos_factory = providers.Factory(Photo)
|
||||||
|
users_factory = providers.Factory(User,
|
||||||
|
photos_factory=photos_factory.provider)
|
||||||
|
|
||||||
|
|
||||||
|
# Creating several User objects:
|
||||||
|
user1 = users_factory() # Same as: user1 = User(photos_factory=photos_factory)
|
||||||
|
user2 = users_factory() # Same as: user2 = User(photos_factory=photos_factory)
|
||||||
|
|
||||||
|
# Making some asserts:
|
||||||
|
assert isinstance(user1.main_photo, Photo)
|
||||||
|
assert isinstance(user2.main_photo, Photo)
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -198,6 +198,14 @@ cdef class Provider(object):
|
||||||
"""
|
"""
|
||||||
return Delegate(self)
|
return Delegate(self)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def provider(self):
|
||||||
|
"""Return provider's delegate.
|
||||||
|
|
||||||
|
:rtype: :py:class:`Delegate`
|
||||||
|
"""
|
||||||
|
return self.delegate()
|
||||||
|
|
||||||
cpdef object _provide(self, tuple args, dict kwargs):
|
cpdef object _provide(self, tuple args, dict kwargs):
|
||||||
"""Providing strategy implementation.
|
"""Providing strategy implementation.
|
||||||
|
|
||||||
|
|
|
@ -32,6 +32,19 @@ class ProviderTests(unittest.TestCase):
|
||||||
|
|
||||||
self.assertIsNot(delegate1, delegate2)
|
self.assertIsNot(delegate1, delegate2)
|
||||||
|
|
||||||
|
def test_provider(self):
|
||||||
|
delegate1 = self.provider.provider
|
||||||
|
|
||||||
|
self.assertIsInstance(delegate1, providers.Delegate)
|
||||||
|
self.assertIs(delegate1(), self.provider)
|
||||||
|
|
||||||
|
delegate2 = self.provider.provider
|
||||||
|
|
||||||
|
self.assertIsInstance(delegate2, providers.Delegate)
|
||||||
|
self.assertIs(delegate2(), self.provider)
|
||||||
|
|
||||||
|
self.assertIsNot(delegate1, delegate2)
|
||||||
|
|
||||||
def test_override(self):
|
def test_override(self):
|
||||||
overriding_provider = providers.Provider()
|
overriding_provider = providers.Provider()
|
||||||
self.provider.override(overriding_provider)
|
self.provider.override(overriding_provider)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user