mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 17:47:02 +03:00
c4b33749d2
* Update callable provider docs * Update coroutine provider docs * Edit object docs * Edit list provider docs * Edit configuration provider docs * Edit selector provider docs * Fix mypy stub of the ``DeclarativeContainer`` to specify the ``__init__`` interface * Edit Dependency provider docs
42 lines
843 B
Python
42 lines
843 B
Python
"""`List` provider example."""
|
|
|
|
import dataclasses
|
|
from typing import List
|
|
|
|
from dependency_injector import providers
|
|
|
|
|
|
@dataclasses.dataclass
|
|
class Module:
|
|
name: str
|
|
|
|
|
|
@dataclasses.dataclass
|
|
class 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 "dispatcher = dispatcher_factory()" is an equivalent for:
|
|
# dispatcher = Dispatcher(
|
|
# modules=[
|
|
# Module(name='m1'),
|
|
# Module(name='m2'),
|
|
# ],
|
|
# )
|