mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-27 20:13:59 +03:00
2beafb9a51
* Add List provider * Add List provider example * Add List provider unit tests * Add docs * Upstream changes from develop * Update API docs * Update unit tests * Add support of positional context argument injections * Update changelog
46 lines
873 B
Python
46 lines
873 B
Python
"""`List` provider example."""
|
|
|
|
import dataclasses
|
|
from typing import List
|
|
|
|
from dependency_injector import providers
|
|
|
|
|
|
@dataclasses.dataclass
|
|
class Module:
|
|
"""Example module."""
|
|
|
|
name: str
|
|
|
|
|
|
@dataclasses.dataclass
|
|
class Dispatcher:
|
|
"""Example dispatcher."""
|
|
|
|
modules: List[Module]
|
|
|
|
|
|
dispatcher_factory = providers.Factory(
|
|
Dispatcher,
|
|
modules=providers.List(
|
|
providers.Factory(Module, name='m1'),
|
|
providers.Factory(Module, name='m2'),
|
|
),
|
|
)
|
|
|
|
if __name__ == '__main__':
|
|
dispatcher = dispatcher_factory()
|
|
|
|
assert isinstance(dispatcher.modules, list)
|
|
assert dispatcher.modules[0].name == 'm1'
|
|
assert dispatcher.modules[1].name == 'm2'
|
|
|
|
# Call of dispatcher_factory() is equivalent to:
|
|
|
|
dispatcher = Dispatcher(
|
|
modules=[
|
|
Module(name='m1'),
|
|
Module(name='m2'),
|
|
],
|
|
)
|