Skip to content

Commit 19600d9

Browse files
committed
Add example for Annotated attribute injection for module/class attributes
1 parent 8bf9ed0 commit 19600d9

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""Wiring attribute example with Annotated."""
2+
3+
from typing import Annotated
4+
5+
from dependency_injector import containers, providers
6+
from dependency_injector.wiring import Provide
7+
8+
9+
class Service:
10+
...
11+
12+
13+
class Container(containers.DeclarativeContainer):
14+
service = providers.Factory(Service)
15+
16+
17+
service: Annotated[Service, Provide[Container.service]]
18+
19+
20+
class Main:
21+
service: Annotated[Service, Provide[Container.service]]
22+
23+
24+
if __name__ == "__main__":
25+
container = Container()
26+
container.wire(modules=[__name__])
27+
28+
assert isinstance(service, Service)
29+
assert isinstance(Main.service, Service)

0 commit comments

Comments
 (0)