mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-06-16 11:33:13 +03:00
Add example for Annotated attribute injection for module/class attributes
This commit is contained in:
parent
8bf9ed04c8
commit
19600d9d08
29
examples/wiring/example_attribute_annotated.py
Normal file
29
examples/wiring/example_attribute_annotated.py
Normal file
|
@ -0,0 +1,29 @@
|
|||
"""Wiring attribute example with Annotated."""
|
||||
|
||||
from typing import Annotated
|
||||
|
||||
from dependency_injector import containers, providers
|
||||
from dependency_injector.wiring import Provide
|
||||
|
||||
|
||||
class Service:
|
||||
...
|
||||
|
||||
|
||||
class Container(containers.DeclarativeContainer):
|
||||
service = providers.Factory(Service)
|
||||
|
||||
|
||||
service: Annotated[Service, Provide[Container.service]]
|
||||
|
||||
|
||||
class Main:
|
||||
service: Annotated[Service, Provide[Container.service]]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
container = Container()
|
||||
container.wire(modules=[__name__])
|
||||
|
||||
assert isinstance(service, Service)
|
||||
assert isinstance(Main.service, Service)
|
Loading…
Reference in New Issue
Block a user