Skip to content

531 Provider import from string #555

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 7 commits into from
Jan 31, 2022
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
4 changes: 4 additions & 0 deletions docs/main/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Development version
``FactoryAggregate.factories`` attribute.
- Add ``.set_providers()`` method to the ``FactoryAggregate`` provider. It is an alias for
``FactoryAggregate.set_factories()`` method.
- Add string imports for ``Factory``, ``Singleton``, ``Callable``, ``Resource``, and ``Coroutine``
providers, e.g. ``Factory("module.Class")``.
See issue `#531 <https://github.com/ets-labs/python-dependency-injector/issues/531>`_.
Thanks to `@al-stefanitsky-mozdor <https://github.com/al-stefanitsky-mozdor>`_ for suggesting the feature.
- Fix ``Dependency`` provider to don't raise "Dependency is not defined" error when the ``default``
is a falsy value of proper type.
See issue `#550 <https://github.com/ets-labs/python-dependency-injector/issues/550>`_. Thanks to
Expand Down
39 changes: 39 additions & 0 deletions docs/providers/factory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,45 @@ attribute of the provider that you're going to inject.

.. note:: Any provider has a ``.provider`` attribute.

.. _factory-string-imports:

String imports
--------------

``Factory`` provider can handle string imports:

.. code-block:: python

class Container(containers.DeclarativeContainer):

service = providers.Factory("myapp.mypackage.mymodule.Service")

You can also make a relative import:

.. code-block:: python

# in myapp/container.py

class Container(containers.DeclarativeContainer):

service = providers.Factory(".mypackage.mymodule.Service")

or import a member of the current module just specifying its name:

.. code-block:: python

class Service:
...


class Container(containers.DeclarativeContainer):

service = providers.Factory("Service")

.. note::
``Singleton``, ``Callable``, ``Resource``, and ``Coroutine`` providers handle string imports
the same way as a ``Factory`` provider.

.. _factory-specialize-provided-type:

Specializing the provided type
Expand Down
Loading