Skip to content

List provider #251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/main/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ follows `Semantic versioning`_
Development version
-------------------

- Add ``List`` provider
`issue #243 <https://github.com/ets-labs/python-dependency-injector/issues/243>`_,
`PR #251 <https://github.com/ets-labs/python-dependency-injector/pull/251>`_.
- Fix a few typos in docs (thanks to `Bruno P. Kinoshita <https://github.com/kinow>`_,
`issue #249 <https://github.com/ets-labs/python-dependency-injector/issues/249>`_,
`PR #250 <https://github.com/ets-labs/python-dependency-injector/pull/250>`_).
Expand Down
1 change: 1 addition & 0 deletions docs/providers/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Providers package API docs - :py:mod:`dependency_injector.providers`
callable
coroutine
object
list
dependency
overriding
custom
34 changes: 34 additions & 0 deletions docs/providers/list.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
List providers
--------------

.. currentmodule:: dependency_injector.providers

:py:class:`List` provider provides a list of values.

.. literalinclude:: ../../examples/providers/list.py
:language: python
:lines: 23-29
:linenos:

: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-29
:linenos:

.. note::

Keyword argument injections are not supported.

.. disqus::
45 changes: 45 additions & 0 deletions examples/providers/list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""`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'),
],
)
Loading