File tree 1 file changed +31
-1
lines changed
1 file changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -254,13 +254,43 @@ To inject a container use special identifier ``<container>``:
254
254
Making injections into modules and class attributes
255
255
---------------------------------------------------
256
256
257
- You can use wiring to make injections into modules and class attributes.
257
+ You can use wiring to make injections into modules and class attributes. Both the classic marker
258
+ syntax and the ``Annotated `` form are supported.
259
+
260
+ Classic marker syntax:
261
+
262
+ .. code-block :: python
263
+
264
+ service: Service = Provide[Container.service]
265
+
266
+ class Main :
267
+ service: Service = Provide[Container.service]
268
+
269
+ Full example of the classic marker syntax:
258
270
259
271
.. literalinclude :: ../examples/wiring/example_attribute.py
260
272
:language: python
261
273
:lines: 3-
262
274
:emphasize-lines: 14,19
263
275
276
+ Annotated form (Python 3.9+):
277
+
278
+ .. code-block :: python
279
+
280
+ from typing import Annotated
281
+
282
+ service: Annotated[Service, Provide[Container.service]]
283
+
284
+ class Main :
285
+ service: Annotated[Service, Provide[Container.service]]
286
+
287
+ Full example of the annotated form:
288
+
289
+ .. literalinclude :: ../examples/wiring/example_attribute_annotated.py
290
+ :language: python
291
+ :lines: 3-
292
+ :emphasize-lines: 16,21
293
+
264
294
You could also use string identifiers to avoid a dependency on a container:
265
295
266
296
.. code-block :: python
You can’t perform that action at this time.
0 commit comments