Skip to content

Commit 457b6de

Browse files
committed
Docs: clarify and format module/class attribute injection for classic and Annotated forms
1 parent 4e77408 commit 457b6de

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

docs/wiring.rst

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,43 @@ To inject a container use special identifier ``<container>``:
254254
Making injections into modules and class attributes
255255
---------------------------------------------------
256256

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:
258270

259271
.. literalinclude:: ../examples/wiring/example_attribute.py
260272
:language: python
261273
:lines: 3-
262274
:emphasize-lines: 14,19
263275

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+
264294
You could also use string identifiers to avoid a dependency on a container:
265295

266296
.. code-block:: python

0 commit comments

Comments
 (0)