{"id":13458907,"url":"https://github.com/adriangb/di","last_synced_at":"2025-04-13T05:08:12.294Z","repository":{"id":39616640,"uuid":"389526885","full_name":"adriangb/di","owner":"adriangb","description":"Pythonic dependency injection","archived":false,"fork":false,"pushed_at":"2023-10-09T21:46:21.000Z","size":6681,"stargazers_count":317,"open_issues_count":12,"forks_count":13,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-13T05:08:00.558Z","etag":null,"topics":["dependency-injector","python"],"latest_commit_sha":null,"homepage":"https://www.adriangb.com/di/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/adriangb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"docs/contributing.md","funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-07-26T06:11:37.000Z","updated_at":"2025-04-02T20:35:00.000Z","dependencies_parsed_at":"2024-06-18T19:58:53.330Z","dependency_job_id":"ce01f509-9931-457a-8580-1eea16610caf","html_url":"https://github.com/adriangb/di","commit_stats":{"total_commits":496,"total_committers":9,"mean_commits":"55.111111111111114","dds":"0.032258064516129004","last_synced_commit":"6388886e7dc376502fe49f0bedf351d5d1e9bd84"},"previous_names":[],"tags_count":163,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adriangb%2Fdi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adriangb%2Fdi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adriangb%2Fdi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adriangb%2Fdi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/adriangb","download_url":"https://codeload.github.com/adriangb/di/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248665747,"owners_count":21142123,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dependency-injector","python"],"created_at":"2024-07-31T09:00:59.571Z","updated_at":"2025-04-13T05:08:12.265Z","avatar_url":"https://github.com/adriangb.png","language":"Python","funding_links":[],"categories":["Software"],"sub_categories":["DI components of Web frameworks"],"readme":"# `di`: dependency injection toolkit\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://github.com/adriangb/di/actions?query=workflow%3ACI%2FCD+event%3Apush+branch%3Amain\" target=\"_blank\"\u003e\n    \u003cimg src=\"https://github.com/adriangb/di/actions/workflows/workflow.yaml/badge.svg?event=push\u0026branch=main\" alt=\"Test\"\u003e\n\u003c/a\u003e\n\u003ca href=\"https://codecov.io/gh/adriangb/di\" target=\"_blank\"\u003e\n    \u003cimg src=\"https://img.shields.io/codecov/c/github/adriangb/di?color=%2334D058\" alt=\"Coverage\"\u003e\n\u003c/a\u003e\n\u003ca href=\"https://pypi.org/project/di\" target=\"_blank\"\u003e\n    \u003cimg src=\"https://img.shields.io/pypi/v/di?color=%2334D058\u0026label=pypi%20package\" alt=\"Package version\"\u003e\n\u003c/a\u003e\n\u003ca href=\"https://pypi.org/project/di\" target=\"_blank\"\u003e\n    \u003cimg src=\"https://img.shields.io/pypi/pyversions/di.svg?color=%2334D058\" alt=\"Supported Python versions\"\u003e\n\u003c/a\u003e\n\u003c/p\u003e\n\n`di` is a modern dependency injection toolkit, modeled around the simplicity of FastAPI's dependency injection.\n\nKey features:\n\n- **Intuitive**: simple API, inspired by [FastAPI].\n- **Auto-wiring**: `di` supports auto-wiring using type annotations.\n- **Scopes**: inspired by [pytest scopes], but defined by users (no fixed \"request\" or \"session\" scopes).\n- **Composable**: decoupled internal APIs give you the flexibility to customize wiring, execution and binding.\n- **Performant**: `di` can execute dependencies in parallel and cache results ins scopes. Performance critical parts are written in [🦀] via [graphlib2].\n\n## Installation\n\n```shell\npip install di[anyio]\n```\n\n⚠️ This project is a work in progress. Until there is 1.X.Y release, expect breaking changes. ⚠️\n\n## Simple Example\n\nHere is a simple example of how `di` works:\n\n```python\nfrom dataclasses import dataclass\n\nfrom di import Container\nfrom di.dependent import Dependent\nfrom di.executors import SyncExecutor\n\n\nclass A:\n    ...\n\n\nclass B:\n    ...\n\n\n@dataclass\nclass C:\n    a: A\n    b: B\n\n\ndef main():\n    container = Container()\n    executor = SyncExecutor()\n    solved = container.solve(Dependent(C, scope=\"request\"), scopes=[\"request\"])\n    with container.enter_scope(\"request\") as state:\n        c = solved.execute_sync(executor=executor, state=state)\n    assert isinstance(c, C)\n    assert isinstance(c.a, A)\n    assert isinstance(c.b, B)\n```\n\nFor more examples, see our [docs].\n\n### Why do I need dependency injection in Python? Isn't that a Java thing?\n\nDependency injection is a software architecture technique that helps us achieve [inversion of control] and [dependency inversion] (one of the five [SOLID] design principles).\n\nIt is a common misconception that traditional software design principles do not apply to Python.\nAs a matter of fact, you are probably using a lot of these techniques already!\n\nFor example, the `transport` argument to httpx's Client ([docs](https://www.python-httpx.org/advanced/#custom-transports)) is an excellent example of dependency injection. Pytest, arguably the most popular Python test framework, uses dependency injection in the form of [pytest fixtures].\n\nMost 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].\n\nFor a more comprehensive overview of Python projects related to dependency injection, see [Awesome Dependency Injection in Python].\n\n## Project Aims\n\nThis project aims to be a dependency injection toolkit, with a focus on providing the underlying dependency injection functionality for other libraries.\n\nIn 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.\n\nFor more background, see our [docs].\n\n[🦀]: https://www.rust-lang.org\n[graphlib2]: https://github.com/adriangb/graphlib2\n[docs]: https://www.adriangb.com/di/\n[binds]: binds.md\n[dependency inversion]: https://en.wikipedia.org/wiki/Dependency_inversion_principle\n[SOLID]: https://en.wikipedia.org/wiki/SOLID\n[inversion of control]: https://en.wikipedia.org/wiki/Inversion_of_control\n[click]: https://click.palletsprojects.com/en/8.0.x/\n[Textual]: https://github.com/willmcgugan/textual\n[FastAPI]: https://fastapi.tiangolo.com/tutorial/dependencies/\n[BlackSheep]: https://www.neoteroi.dev/blacksheep/dependency-injection/\n[Awesome Dependency Injection in Python]: https://github.com/sfermigier/awesome-dependency-injection-in-python\n[python-dependency-injector]: https://github.com/ets-labs/python-dependency-injector\n[pytest scopes]: https://docs.pytest.org/en/6.2.x/fixture.html#scope-sharing-fixtures-across-classes-modules-packages-or-session\n[pytest fixtures]: https://docs.pytest.org/en/6.2.x/fixture.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadriangb%2Fdi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadriangb%2Fdi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadriangb%2Fdi/lists"}