Skip to content

ets-labs/python-dependency-injector

Repository files navigation


Latest Version License Supported Python versions Supported Python implementations Downloads Downloads Downloads Wheel Build Status Docs Status Coverage Status

What is Dependency Injector?

Dependency Injector is a dependency injection framework for Python.

Why do I need it?

Dependency Injector helps you understand and change the structure of the application.

With the Dependency Injector you keep application structure in one place. This place is called the container. You use the container to manage all the components of the application. All the component dependencies are defined explicitly. This provides the control on the application structure. It is easy to understand and change it.

The container is like a map of your application. You always know what depends on what.

Flask + Dependency Injector example application container:

from dependency_injector import containers, providers from dependency_injector.ext import flask from flask import Flask from flask_bootstrap import Bootstrap from github import Github from . import views, services class ApplicationContainer(containers.DeclarativeContainer): """Application container.""" app = flask.Application(Flask, __name__) bootstrap = flask.Extension(Bootstrap) config = providers.Configuration() github_client = providers.Factory( Github, login_or_token=config.github.auth_token, timeout=config.github.request_timeout, ) search_service = providers.Factory( services.SearchService, github_client=github_client, ) index_view = flask.View( views.index, search_service=search_service, default_search_term=config.search.default_term, default_search_limit=config.search.default_limit, )

Running such container looks like this:

from .containers import ApplicationContainer def create_app(): """Create and return Flask application.""" container = ApplicationContainer() container.config.from_yaml('config.yml') container.config.github.auth_token.from_env('GITHUB_TOKEN') app = container.app() app.container = container bootstrap = container.bootstrap() bootstrap.init_app(app) app.add_url_rule('/', view_func=container.index_view.as_view()) return app

And testing looks like:

from unittest import mock import pytest from github import Github from flask import url_for from .application import create_app @pytest.fixture def app(): return create_app() def test_index(client, app): github_client_mock = mock.Mock(spec=Github) # Configure mock with app.container.github_client.override(github_client_mock): response = client.get(url_for('index')) assert response.status_code == 200 # Do more asserts

See complete example here - Flask + Dependency Injector Example

How to install?

  • The package is available on the PyPi:

    pip install dependency-injector 

Where is the docs?

Have a question?

Found a bug?

Want to help?

  • ⭐️ Star the Dependency Injector on the Github
  • 🆕 Start a new project with the Dependency Injector
  • 💬 Tell your friend about the Dependency Injector

Want to contribute?

  • 🔀 Fork the project
  • ⬅️ Open a pull request to the develop branch