mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-06-17 20:13:13 +03:00
Docs: clarify and format module/class attribute injection for classic and Annotated forms
This commit is contained in:
parent
4e774084bc
commit
457b6de00c
|
@ -254,13 +254,43 @@ To inject a container use special identifier ``<container>``:
|
||||||
Making injections into modules and class attributes
|
Making injections into modules and class attributes
|
||||||
---------------------------------------------------
|
---------------------------------------------------
|
||||||
|
|
||||||
You can use wiring to make injections into modules and class attributes.
|
You can use wiring to make injections into modules and class attributes. Both the classic marker
|
||||||
|
syntax and the ``Annotated`` form are supported.
|
||||||
|
|
||||||
|
Classic marker syntax:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
service: Service = Provide[Container.service]
|
||||||
|
|
||||||
|
class Main:
|
||||||
|
service: Service = Provide[Container.service]
|
||||||
|
|
||||||
|
Full example of the classic marker syntax:
|
||||||
|
|
||||||
.. literalinclude:: ../examples/wiring/example_attribute.py
|
.. literalinclude:: ../examples/wiring/example_attribute.py
|
||||||
:language: python
|
:language: python
|
||||||
:lines: 3-
|
:lines: 3-
|
||||||
:emphasize-lines: 14,19
|
:emphasize-lines: 14,19
|
||||||
|
|
||||||
|
Annotated form (Python 3.9+):
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
from typing import Annotated
|
||||||
|
|
||||||
|
service: Annotated[Service, Provide[Container.service]]
|
||||||
|
|
||||||
|
class Main:
|
||||||
|
service: Annotated[Service, Provide[Container.service]]
|
||||||
|
|
||||||
|
Full example of the annotated form:
|
||||||
|
|
||||||
|
.. literalinclude:: ../examples/wiring/example_attribute_annotated.py
|
||||||
|
:language: python
|
||||||
|
:lines: 3-
|
||||||
|
:emphasize-lines: 16,21
|
||||||
|
|
||||||
You could also use string identifiers to avoid a dependency on a container:
|
You could also use string identifiers to avoid a dependency on a container:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
Loading…
Reference in New Issue
Block a user