python-dependency-injector/examples/providers/dependency_undefined_error.py

32 lines
622 B
Python
Raw Permalink Normal View History

2021-02-13 17:07:22 +03:00
"""`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,
)
2021-09-30 22:32:21 +03:00
if __name__ == "__main__":
2021-02-13 17:07:22 +03:00
container = Container()
container.user_service() # <-- raises error:
# Dependency "Container.database" is not defined