mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-05-23 23:09:14 +03:00
Edit list provider docs
This commit is contained in:
parent
d573c990b0
commit
a5e5b4b193
|
@ -10,7 +10,7 @@ follows `Semantic versioning`_
|
|||
Development version
|
||||
-------------------
|
||||
- Update documentation and rework examples for: ``Singleton``, ``Callable``, ``Coroutine``,
|
||||
``Object`` providers.
|
||||
``Object``, ``List`` providers.
|
||||
|
||||
3.34.0
|
||||
------
|
||||
|
|
|
@ -1,5 +1,10 @@
|
|||
List providers
|
||||
--------------
|
||||
List provider
|
||||
-------------
|
||||
|
||||
.. meta::
|
||||
:keywords: Python,DI,Dependency injection,IoC,Inversion of Control,List,Injection
|
||||
:description: List provider helps to inject a list of the dependencies. This page demonstrates
|
||||
how to use a List provider.
|
||||
|
||||
.. currentmodule:: dependency_injector.providers
|
||||
|
||||
|
@ -7,28 +12,12 @@ List providers
|
|||
|
||||
.. literalinclude:: ../../examples/providers/list.py
|
||||
:language: python
|
||||
:emphasize-lines: 6-9
|
||||
:lines: 6-8, 23-29
|
||||
|
||||
:py:class:`List` provider is needed for injecting a list of dependencies. It handles
|
||||
positional argument injections the same way as :py:class:`Factory` provider:
|
||||
|
||||
+ All providers (instances of :py:class:`Provider`) are called every time
|
||||
when injection needs to be done.
|
||||
+ Providers could be injected "as is" (delegated), if it is defined explicitly. Check out
|
||||
:ref:`factory_providers_delegation`.
|
||||
+ All other values are injected *"as is"*.
|
||||
+ Positional context arguments will be appended after :py:class:`List` positional injections.
|
||||
|
||||
Full example:
|
||||
|
||||
.. literalinclude:: ../../examples/providers/list.py
|
||||
:language: python
|
||||
:emphasize-lines: 23-26
|
||||
:lines: 3-
|
||||
:emphasize-lines: 19-22
|
||||
|
||||
``List`` provider handles positional arguments the same way as a :ref:`factory-provider`.
|
||||
|
||||
.. note::
|
||||
|
||||
Keyword argument injections are not supported.
|
||||
Keyword argument are not supported.
|
||||
|
||||
.. disqus::
|
||||
|
|
|
@ -8,15 +8,11 @@ from dependency_injector import providers
|
|||
|
||||
@dataclasses.dataclass
|
||||
class Module:
|
||||
"""Example module."""
|
||||
|
||||
name: str
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class Dispatcher:
|
||||
"""Example dispatcher."""
|
||||
|
||||
modules: List[Module]
|
||||
|
||||
|
||||
|
@ -28,6 +24,7 @@ dispatcher_factory = providers.Factory(
|
|||
),
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
dispatcher = dispatcher_factory()
|
||||
|
||||
|
@ -35,11 +32,10 @@ if __name__ == '__main__':
|
|||
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'),
|
||||
],
|
||||
)
|
||||
# Call "dispatcher = dispatcher_factory()" is an equivalent for:
|
||||
# dispatcher = Dispatcher(
|
||||
# modules=[
|
||||
# Module(name='m1'),
|
||||
# Module(name='m2'),
|
||||
# ],
|
||||
# )
|
||||
|
|
Loading…
Reference in New Issue
Block a user