di

Pythonic dependency injection

https://github.com/adriangb/di

Science Score: 13.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (15.3%) to scientific vocabulary

Keywords

dependency-injector python

Keywords from Contributors

pydantic asgi redoc asyncio python39 python313 python312 python311 python310 parsing
Last synced: 6 months ago · JSON representation

Repository

Pythonic dependency injection

Basic Info
Statistics
  • Stars: 324
  • Watchers: 5
  • Forks: 13
  • Open Issues: 12
  • Releases: 163
Topics
dependency-injector python
Created over 4 years ago · Last pushed over 2 years ago
Metadata Files
Readme Contributing License Code of conduct

README.md

di: dependency injection toolkit

Test Coverage Package version Supported Python versions

di is a modern dependency injection toolkit, modeled around the simplicity of FastAPI's dependency injection.

Key features:

  • Intuitive: simple API, inspired by FastAPI.
  • Auto-wiring: di supports auto-wiring using type annotations.
  • Scopes: inspired by pytest scopes, but defined by users (no fixed "request" or "session" scopes).
  • Composable: decoupled internal APIs give you the flexibility to customize wiring, execution and binding.
  • Performant: di can execute dependencies in parallel and cache results ins scopes. Performance critical parts are written in 🦀 via graphlib2.

Installation

shell pip install di[anyio]

⚠️ This project is a work in progress. Until there is 1.X.Y release, expect breaking changes. ⚠️

Simple Example

Here is a simple example of how di works:

```python from dataclasses import dataclass

from di import Container from di.dependent import Dependent from di.executors import SyncExecutor

class A: ...

class B: ...

@dataclass class C: a: A b: B

def main(): container = Container() executor = SyncExecutor() solved = container.solve(Dependent(C, scope="request"), scopes=["request"]) with container.enterscope("request") as state: c = solved.executesync(executor=executor, state=state) assert isinstance(c, C) assert isinstance(c.a, A) assert isinstance(c.b, B) ```

For more examples, see our docs.

Why do I need dependency injection in Python? Isn't that a Java thing?

Dependency injection is a software architecture technique that helps us achieve inversion of control and dependency inversion.

It is a common misconception that traditional software design principles do not apply to Python. As a matter of fact, you are probably using a lot of these techniques already!

For example, the transport argument to httpx's Client (docs) is an excellent example of dependency injection. Pytest, arguably the most popular Python test framework, uses dependency injection in the form of pytest fixtures.

Most web frameworks employ inversion of control: when you define a view / controller, the web framework calls you! The same thing applies to CLIs (like click) or TUIs (like Textual). This is especially true for many newer web frameworks that not only use inversion of control but also dependency injection. Two great examples of this are FastAPI and BlackSheep.

For a more comprehensive overview of Python projects related to dependency injection, see Awesome Dependency Injection in Python.

Project Aims

This project aims to be a dependency injection toolkit, with a focus on providing the underlying dependency injection functionality for other libraries.

In other words, while you could use this as a standalone dependency injection framework, you may find it to be a bit terse and verbose. There are also much more mature standalone dependency injection frameworks; I would recommend at least looking into python-dependency-injector since it is currently the most popular / widely used of the bunch.

For more background, see our docs.

Owner

  • Name: Adrian Garcia Badaracco
  • Login: adriangb
  • Kind: user
  • Location: Chicago, IL

GitHub Events

Total
  • Issues event: 1
  • Watch event: 27
Last Year
  • Issues event: 1
  • Watch event: 27

Committers

Last synced: 10 months ago

All Time
  • Total Commits: 496
  • Total Committers: 9
  • Avg Commits per committer: 55.111
  • Development Distribution Score (DDS): 0.032
Past Year
  • Commits: 0
  • Committers: 0
  • Avg Commits per committer: 0.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Adrian Garcia Badaracco 1****b 480
Max Zhenzhera 5****a 7
Santiago s****y@g****m 3
Thomas Grainger t****n@g****m 1
Sondre Lillebø Gundersen s****g@l****o 1
SamWarden 5****n 1
Marcelo Trylesinski m****e@g****m 1
Guilhem C g****s@g****m 1
Eclips4 8****4 1
Committer Domains (Top 20 + Academic)
live.no: 1

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 49
  • Total pull requests: 66
  • Average time to close issues: 23 days
  • Average time to close pull requests: 14 days
  • Total issue authors: 10
  • Total pull request authors: 11
  • Average comments per issue: 1.59
  • Average comments per pull request: 1.64
  • Merged pull requests: 53
  • Bot issues: 0
  • Bot pull requests: 2
Past Year
  • Issues: 1
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 1
  • Pull request authors: 0
  • Average comments per issue: 0.0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • adriangb (34)
  • maxzhenzhera (6)
  • woile (2)
  • Lancetnik (1)
  • jab (1)
  • aliaksei-imi (1)
  • dineshbvadhia (1)
  • Eclips4 (1)
  • jriddy (1)
Pull Request Authors
  • adriangb (47)
  • maxzhenzhera (5)
  • woile (3)
  • Lancetnik (1)
  • graingert (1)
  • sondrelg (1)
  • SamWarden (1)
  • dependabot[bot] (1)
  • Eclips4 (1)
  • chagui (1)
  • Kludex (1)
Top Labels
Issue Labels
Pull Request Labels
dependencies (1)

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 14,570 last-month
  • Total docker downloads: 158
  • Total dependent packages: 1
  • Total dependent repositories: 32
  • Total versions: 171
  • Total maintainers: 2
pypi.org: di

Dependency injection toolkit

  • Versions: 171
  • Dependent Packages: 1
  • Dependent Repositories: 32
  • Downloads: 14,570 Last month
  • Docker Downloads: 158
Rankings
Docker downloads count: 1.6%
Dependent repos count: 2.6%
Stargazers count: 4.3%
Dependent packages count: 4.8%
Average: 4.9%
Downloads: 5.4%
Forks count: 10.5%
Maintainers (2)
Last synced: 6 months ago

Dependencies

.github/workflows/workflow.yaml actions
  • actions/checkout v2 composite
  • actions/setup-python v2 composite
  • codecov/codecov-action v2.1.0 composite
  • ncipollo/release-action v1 composite
  • snok/install-poetry v1.3.1 composite
  • snok/install-poetry v1.3.0 composite
poetry.lock pypi
  • async-generator 1.10 develop
  • atomicwrites 1.4.0 develop
  • attrs 21.4.0 develop
  • black 22.6.0 develop
  • cached-property 1.5.2 develop
  • cffi 1.15.1 develop
  • cfgv 3.3.1 develop
  • click 8.1.3 develop
  • colorama 0.4.5 develop
  • coverage 6.4.1 develop
  • distlib 0.3.4 develop
  • filelock 3.7.1 develop
  • flake8 3.9.2 develop
  • ghp-import 2.1.0 develop
  • griffe 0.22.1 develop
  • identify 2.5.1 develop
  • importlib-metadata 4.12.0 develop
  • iniconfig 1.1.1 develop
  • isort 5.10.1 develop
  • jinja2 3.1.2 develop
  • markdown 3.3.7 develop
  • markupsafe 2.1.1 develop
  • mccabe 0.6.1 develop
  • mergedeep 1.3.4 develop
  • mike 1.1.2 develop
  • mkdocs 1.3.0 develop
  • mkdocs-autorefs 0.4.1 develop
  • mkdocs-material 8.3.9 develop
  • mkdocs-material-extensions 1.0.3 develop
  • mkdocstrings 0.19.0 develop
  • mkdocstrings-python 0.7.1 develop
  • mypy 0.940 develop
  • mypy-extensions 0.4.3 develop
  • nodeenv 1.7.0 develop
  • outcome 1.2.0 develop
  • packaging 21.3 develop
  • pathspec 0.9.0 develop
  • platformdirs 2.5.2 develop
  • pluggy 1.0.0 develop
  • pre-commit 2.19.0 develop
  • py 1.11.0 develop
  • pycodestyle 2.7.0 develop
  • pycparser 2.21 develop
  • pyflakes 2.3.1 develop
  • pygments 2.12.0 develop
  • pyinstrument 4.2.0 develop
  • pymdown-extensions 9.5 develop
  • pyparsing 3.0.9 develop
  • pytest 6.2.5 develop
  • pytest-cov 2.12.1 develop
  • pytest-sugar 0.9.4 develop
  • python-dateutil 2.8.2 develop
  • pyyaml 6.0 develop
  • pyyaml-env-tag 0.1 develop
  • six 1.16.0 develop
  • sortedcontainers 2.4.0 develop
  • termcolor 1.1.0 develop
  • toml 0.10.2 develop
  • tomli 2.0.1 develop
  • trio 0.21.0 develop
  • typed-ast 1.5.4 develop
  • verspec 0.1.0 develop
  • virtualenv 20.15.1 develop
  • watchdog 2.1.9 develop
  • zipp 3.8.0 develop
  • anyio 3.6.1
  • graphlib2 0.4.5
  • idna 3.3
  • sniffio 1.2.0
  • typing-extensions 4.3.0
pyproject.toml pypi
  • black ~22 develop
  • coverage ^6.0 develop
  • flake8 ~3.9 develop
  • isort ~5.10 develop
  • mike ~1 develop
  • mkdocs ~1 develop
  • mkdocs-material ~8,!=8.1.3 develop
  • mkdocstrings ^0.19.0 develop
  • mypy v0.940 develop
  • pre-commit ~2 develop
  • pyinstrument ~4 develop
  • pytest ~6 develop
  • pytest-cov ~2 develop
  • pytest-sugar ~0 develop
  • trio ~0 develop
  • anyio >=3.5.0
  • graphlib2 ^0.4.1
  • python >=3.7,<4
  • typing-extensions >=3