-
-
Notifications
You must be signed in to change notification settings - Fork 322
/
Copy pathlist.py
45 lines (31 loc) · 979 Bytes
/
list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""`List` provider example."""
import dataclasses
from typing import List
from dependency_injector import containers, providers
@dataclasses.dataclass
class Module:
name: str
@dataclasses.dataclass
class Dispatcher:
modules: List[Module]
class Container(containers.DeclarativeContainer):
dispatcher_factory = providers.Factory(
Dispatcher,
modules=providers.List(
providers.Factory(Module, name="m1"),
providers.Factory(Module, name="m2"),
),
)
if __name__ == "__main__":
container = Container()
dispatcher = container.dispatcher_factory()
assert isinstance(dispatcher.modules, list)
assert dispatcher.modules[0].name == "m1"
assert dispatcher.modules[1].name == "m2"
# Call "dispatcher = container.dispatcher_factory()" is equivalent to:
# dispatcher = Dispatcher(
# modules=[
# Module(name="m1"),
# Module(name="m2"),
# ],
# )