Skip to content

Commit aeace8c

Browse files
committed
Update documentation on injecting provided object attributes, items or method calls
1 parent e4ca126 commit aeace8c

File tree

5 files changed

+22
-29
lines changed

5 files changed

+22
-29
lines changed

docs/main/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ follows `Semantic versioning`_
1010
Development version
1111
-------------------
1212
- Update providers overriding documentation and rework examples.
13+
- Update documentation on injecting provided object attributes, items or method calls.
1314

1415
3.35.1
1516
------

docs/providers/overriding.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Provider overriding
1010

1111
.. currentmodule:: dependency_injector.providers
1212

13-
Any provider can be overridden by another provider.
13+
You can override any provider by another provider.
1414

1515
When provider is overridden it calls to the overriding provider instead of providing
1616
the object by its own.
Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1-
Injecting attributes, items, or call methods of the provided instance
2-
=====================================================================
1+
Injecting provided object attributes, items, or call its methods
2+
================================================================
33

4-
.. currentmodule:: dependency_injector.providers
4+
.. meta::
5+
:keywords: Python,DI,Dependency injection,IoC,Inversion of Control,Attribute,Method,Call
6+
:description: This page demonstrates how to inject attributes, items or call method of the
7+
provided instance.
58

6-
In this section you will know how to inject provided instance attribute or item into the other
7-
provider.
9+
.. currentmodule:: dependency_injector.providers
810

9-
It also describes how to call a method of the provided instance and use the result of
10-
this call as an injection value.
11+
You can inject provided object attribute, item or result of its method call.
1112

1213
.. literalinclude:: ../../examples/providers/provided_instance.py
1314
:language: python
1415
:emphasize-lines: 26-32
1516
:lines: 3-
1617

1718
To use the feature you should use the ``.provided`` attribute of the injected provider. This
18-
attribute helps to specify what happens with the provided instance. You can retrieve an injection
19-
value from:
19+
attribute helps to specify what happens with the provided instance before the injection. You can
20+
use any combination of the following:
2021

21-
- an attribute of the provided instance
22-
- an item of the provided instance
23-
- a call of the provided instance method
22+
- an attribute of the provided object
23+
- an item of the provided object
24+
- a call of the provided object method
2425

25-
When you use the call of the provided instance method you can specify the injections into this
26+
When you use a call of the provided instance method you can specify the injections for this
2627
method like you do with any other provider.
2728

2829
You can do nested constructions:
@@ -32,35 +33,24 @@ You can do nested constructions:
3233
:emphasize-lines: 24-30
3334
:lines: 3-
3435

35-
Attribute ``.provided`` is available for the providers that return instances. Providers that
36-
have ``.provided`` attribute:
36+
The ``.provided`` attribute is available for the next providers:
3737

38-
- :py:class:`Callable` and its subclasses
3938
- :py:class:`Factory` and its subclasses
4039
- :py:class:`Singleton` and its subclasses
40+
- :py:class:`Callable` and its subclasses
4141
- :py:class:`Object`
4242
- :py:class:`List`
4343
- :py:class:`Selector`
4444
- :py:class:`Dependency`
4545

46-
Special providers like :py:class:`Configuration` or :py:class:`Delegate` do not have the
47-
``.provided`` attribute.
48-
49-
Provider subclasses
50-
-------------------
51-
5246
When you create a new provider subclass and want to implement the ``.provided`` attribute, you
53-
should use the :py:class:`ProvidedInstance` provider.
47+
should use the :py:class:`ProvidedInstance` provider. Add the ``.provided`` property
48+
implementation to a new subclass:
5449

5550
.. code-block:: python
5651
5752
@property
5853
def provided(self):
59-
"""Return :py:class:`ProvidedInstance` provider."""
6054
return ProvidedInstance(self)
6155
62-
In all other cases you should not use :py:class:`ProvidedInstance`, :py:class:`AttributeGetter`,
63-
:py:class:`ItemGetter`, or :py:class:`MethodCaller` providers directly. Use the ``.provided``
64-
attribute of the injected provider instead.
65-
6656
.. disqus::

examples/providers/provided_instance.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def __init__(self, value1, value2, value3, value4):
3333
value4=service.provided.get_value.call(),
3434
)
3535

36+
3637
if __name__ == '__main__':
3738
client = client_factory()
3839
assert client.value1 == client.value2 == client.value3 == 'foo'

examples/providers/provided_instance_complex.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def get_value(self):
3131
dependency.provided['foo']['baz'].call(service)['arg'].get_value.call(),
3232
)
3333

34+
3435
if __name__ == '__main__':
3536
assert demo_list() == [
3637
10,

0 commit comments

Comments
 (0)