Skip to content

Commit d3720bd

Browse files
committed
Merge branch 'release/4.26.0' into master
2 parents 0026f48 + 0149338 commit d3720bd

File tree

18 files changed

+1040
-94
lines changed

18 files changed

+1040
-94
lines changed

docs/main/changelog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ that were made in every particular version.
77
From version 0.7.6 *Dependency Injector* framework strictly
88
follows `Semantic versioning`_
99

10+
4.26.0
11+
------
12+
- Add wiring by string id.
13+
- Improve error message for ``Dependency`` provider missing attribute.
14+
1015
4.25.1
1116
------
1217
- Amend docs and add another example for ``@containers.copy()`` decorator.

docs/wiring.rst

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,82 @@ Also you can use ``Provide`` marker to inject a container.
8888
:emphasize-lines: 16-19
8989
:lines: 3-
9090

91+
Strings identifiers
92+
-------------------
93+
94+
You can use wiring with string identifiers. String identifier should match provider name in the container:
95+
96+
.. literalinclude:: ../examples/wiring/example_string_id.py
97+
:language: python
98+
:emphasize-lines: 17
99+
:lines: 3-
100+
101+
With string identifiers you don't need to use a container to specify an injection.
102+
103+
To specify an injection from a nested container use point ``.`` as a separator:
104+
105+
.. code-block:: python
106+
107+
@inject
108+
def foo(service: UserService = Provide['services.user']) -> None:
109+
...
110+
111+
You can also use injection modifiers:
112+
113+
.. code-block:: python
114+
115+
from dependency_injector.wiring import (
116+
inject,
117+
Provide,
118+
as_int,
119+
as_float,
120+
as_,
121+
required,
122+
invariant,
123+
provided,
124+
)
125+
126+
127+
@inject
128+
def foo(value: int = Provide['config.option', as_int()]) -> None:
129+
...
130+
131+
132+
@inject
133+
def foo(value: float = Provide['config.option', as_float()]) -> None:
134+
...
135+
136+
137+
@inject
138+
def foo(value: Decimal = Provide['config.option', as_(Decimal)]) -> None:
139+
...
140+
141+
@inject
142+
def foo(value: str = Provide['config.option', required()]) -> None:
143+
...
144+
145+
@inject
146+
def foo(value: int = Provide['config.option', required().as_int()]) -> None:
147+
...
148+
149+
150+
@inject
151+
def foo(value: int = Provide['config.option', invariant('config.switch')]) -> None:
152+
...
153+
154+
@inject
155+
def foo(value: int = Provide['service', provided().foo['bar'].call()]) -> None:
156+
...
157+
158+
159+
To inject a container use special identifier ``<container>``:
160+
161+
.. code-block:: python
162+
163+
@inject
164+
def foo(container: Container = Provide['<container>']) -> None:
165+
...
166+
91167
Wiring with modules and packages
92168
--------------------------------
93169

examples/wiring/example_string_id.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""Wiring string id example."""
2+
3+
import sys
4+
5+
from dependency_injector import containers, providers
6+
from dependency_injector.wiring import inject, Provide
7+
8+
9+
class Service:
10+
...
11+
12+
13+
class Container(containers.DeclarativeContainer):
14+
15+
service = providers.Factory(Service)
16+
17+
18+
@inject
19+
def main(service: Service = Provide['service']) -> None:
20+
...
21+
22+
23+
if __name__ == '__main__':
24+
container = Container()
25+
container.wire(modules=[sys.modules[__name__]])
26+
27+
main()

src/dependency_injector/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Top-level package."""
22

3-
__version__ = '4.25.1'
3+
__version__ = '4.26.0'
44
"""Version number.
55
66
:type: str

0 commit comments

Comments
 (0)