mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 17:47:02 +03:00
839a319831
* Add prototype for flat resolving * Add working prototype for sample 1 and 3 * Add working prototype, requires deep refactoring * Update DependenciesContainer to handle Contrainer provider * Fix Dependency provider copying issue * Add hardening fix for Self provider to avoid copying bugs * Fix flaky container copy issue * Rename set_parent() to assign_parent() * Refactor Dependency provider and its typing stub * Add tests for Dependency provider * Update makefile to run coverage when tests fail * Clean up DependenciesContainer provider and add tests * Clean up Container provider and add tests * Clean up container instance and add tests * Refactor isinstance() checks * Clean up DeclarativeContainer and add tests * Update docs and examples * Update changelog * Revoke makefile change
35 lines
1.3 KiB
ReStructuredText
35 lines
1.3 KiB
ReStructuredText
Dependency provider
|
|
===================
|
|
|
|
.. currentmodule:: dependency_injector.providers
|
|
|
|
:py:class:`Dependency` provider is a placeholder for a dependency of a certain type.
|
|
|
|
To specify a type of the dependency use ``instance_of`` argument: ``Dependency(instance_of=SomeClass)``.
|
|
Dependency provider will control that returned object is an instance of ``instance_of`` type.
|
|
|
|
.. literalinclude:: ../../examples/providers/dependency.py
|
|
:language: python
|
|
:lines: 3-
|
|
:emphasize-lines: 26,35-36
|
|
|
|
To provide a dependency you need to override the ``Dependency`` provider. You can call
|
|
provider ``.override()`` method or provide an overriding provider when creating a container.
|
|
See :ref:`provider-overriding`. If you don't provide a dependency, ``Dependency`` provider
|
|
will raise an error:
|
|
|
|
.. literalinclude:: ../../examples/providers/dependency_undefined_error.py
|
|
:language: python
|
|
:lines: 18-
|
|
|
|
You also can provide a default for the dependency. To provide a default use ``default`` argument:
|
|
``Dependency(..., default=...)``. Default can be a value or a provider. If default is not a provider,
|
|
dependency provider will wrap it into the ``Object`` provider.
|
|
|
|
.. literalinclude:: ../../examples/providers/dependency_default.py
|
|
:language: python
|
|
:lines: 16-23
|
|
:emphasize-lines: 3
|
|
|
|
.. disqus::
|