mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-25 11:04:01 +03:00
Update docs and examples
This commit is contained in:
parent
21c0c82144
commit
50d05d43c6
|
@ -15,7 +15,12 @@ Dependency provider will control that returned object is an instance of ``instan
|
||||||
|
|
||||||
To provide a dependency you need to override the ``Dependency`` provider. You can call
|
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.
|
provider ``.override()`` method or provide an overriding provider when creating a container.
|
||||||
See :ref:`provider-overriding`.
|
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:
|
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(..., default=...)``. Default can be a value or a provider. If default is not a provider,
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
import abc
|
import abc
|
||||||
import dataclasses
|
import dataclasses
|
||||||
|
|
||||||
from dependency_injector import containers, providers, errors
|
from dependency_injector import containers, providers
|
||||||
|
|
||||||
|
|
||||||
class DbAdapter(metaclass=abc.ABCMeta):
|
class DbAdapter(metaclass=abc.ABCMeta):
|
||||||
|
@ -41,10 +41,5 @@ if __name__ == '__main__':
|
||||||
assert isinstance(container2.user_service().database, PostgresDbAdapter)
|
assert isinstance(container2.user_service().database, PostgresDbAdapter)
|
||||||
|
|
||||||
container3 = Container(database=providers.Singleton(object))
|
container3 = Container(database=providers.Singleton(object))
|
||||||
try:
|
container3.user_service() # <-- raises error:
|
||||||
container3.user_service()
|
# <object ...> is not an instance of DbAdapter
|
||||||
except errors.Error as exception:
|
|
||||||
print(exception)
|
|
||||||
# The output is:
|
|
||||||
# <object object at 0x107ce5c40> is not an
|
|
||||||
# instance of <class '__main__.DbAdapter'>
|
|
||||||
|
|
31
examples/providers/dependency_undefined_error.py
Normal file
31
examples/providers/dependency_undefined_error.py
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
"""`Dependency` provider undefined error example."""
|
||||||
|
|
||||||
|
import abc
|
||||||
|
import dataclasses
|
||||||
|
|
||||||
|
from dependency_injector import containers, providers
|
||||||
|
|
||||||
|
|
||||||
|
class DbAdapter(metaclass=abc.ABCMeta):
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
@dataclasses.dataclass
|
||||||
|
class UserService:
|
||||||
|
database: DbAdapter
|
||||||
|
|
||||||
|
|
||||||
|
class Container(containers.DeclarativeContainer):
|
||||||
|
|
||||||
|
database = providers.Dependency(instance_of=DbAdapter)
|
||||||
|
|
||||||
|
user_service = providers.Factory(
|
||||||
|
UserService,
|
||||||
|
database=database,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
container = Container()
|
||||||
|
container.user_service() # <-- raises error:
|
||||||
|
# Dependency "Container.database" is not defined
|
Loading…
Reference in New Issue
Block a user