Skip to content

Commit

Permalink
Merge abaec3e into d8aa70c
Browse files Browse the repository at this point in the history
  • Loading branch information
rmk135 committed Sep 25, 2021
2 parents d8aa70c + abaec3e commit d62c9db
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/main/changelog.rst
Expand Up @@ -10,6 +10,7 @@ follows `Semantic versioning`_

Develop
-------
- Fix a wiring bug with improper resolving of ``Provide[some_provider.provider]``.
- Fix a typo in ``Factory`` provider docs ``service.add_attributes(clent=client)``
`#499 <https://github.com/ets-labs/python-dependency-injector/issues/499>`_.
Thanks to `@rajanjha786 <https://github.com/rajanjha786>`_ for the contribution.
Expand Down
20 changes: 15 additions & 5 deletions docs/wiring.rst
Expand Up @@ -39,19 +39,29 @@ a function or method argument:
Specifying an annotation is optional.

There are two types of markers:
To inject the provider itself use ``Provide[foo.provider]``:

- ``Provide[foo]`` - call the provider ``foo`` and injects the result
- ``Provider[foo]`` - injects the provider ``foo`` itself
.. code-block:: python
from dependency_injector.providers import Factory
from dependency_injector.wiring import inject, Provide
@inject
def foo(bar_provider: Factory[Bar] = Provide[Container.bar.provider]):
bar = bar_provider(argument="baz")
...
You can also use ``Provider[foo]`` for injecting the provider itself:

.. code-block:: python
from dependency_injector.providers import Factory
from dependency_injector.wiring import inject, Provider
@inject
def foo(bar_provider: Callable[..., Bar] = Provider[Container.bar]):
bar = bar_provider()
def foo(bar_provider: Factory[Bar] = Provider[Container.bar]):
bar = bar_provider(argument="baz")
...
You can use configuration, provided instance and sub-container providers as you normally do.
Expand Down
10 changes: 8 additions & 2 deletions src/dependency_injector/wiring.py
Expand Up @@ -226,7 +226,10 @@ def _resolve_delegate(
self,
original: providers.Delegate,
) -> Optional[providers.Provider]:
return self._resolve_provider(original.provides)
provider = self._resolve_provider(original.provides)
if provider:
provider = provider.provider
return provider

def _resolve_config_option(
self,
Expand Down Expand Up @@ -539,7 +542,10 @@ def _bind_injections(fn: Callable[..., Any], providers_map: ProvidersMap) -> Non
if isinstance(marker, Provide):
fn.__injections__[injection] = provider
elif isinstance(marker, Provider):
fn.__injections__[injection] = provider.provider
if isinstance(provider, providers.Delegate):
fn.__injections__[injection] = provider
else:
fn.__injections__[injection] = provider.provider

if injection in fn.__reference_closing__:
fn.__closing__[injection] = provider
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/samples/wiringsamples/module.py
Expand Up @@ -84,7 +84,13 @@ def test_config_value_required_undefined(


@inject
def test_provide_provider(service_provider: Callable[..., Service] = Provider[Container.service.provider]):
def test_provide_provider(service_provider: Callable[..., Service] = Provide[Container.service.provider]):
service = service_provider()
return service


@inject
def test_provider_provider(service_provider: Callable[..., Service] = Provider[Container.service.provider]):
service = service_provider()
return service

Expand Down
4 changes: 4 additions & 0 deletions tests/unit/wiring/test_wiring_py36.py
Expand Up @@ -169,6 +169,10 @@ def test_provide_provider(self):
service = module.test_provide_provider()
self.assertIsInstance(service, Service)

def test_provider_provider(self):
service = module.test_provider_provider()
self.assertIsInstance(service, Service)

def test_provided_instance(self):
class TestService:
foo = {
Expand Down

0 comments on commit d62c9db

Please sign in to comment.