https://github.com/100nm/python-injection
Fast and easy dependency injection framework.
https://github.com/100nm/python-injection
async async-python decorators decorators-python dependency-injection fastapi mypy python type-hints typed
Last synced: 8 months ago
JSON representation
Fast and easy dependency injection framework.
- Host: GitHub
- URL: https://github.com/100nm/python-injection
- Owner: 100nm
- Created: 2023-07-13T15:11:15.000Z (over 2 years ago)
- Default Branch: prod
- Last Pushed: 2025-04-30T10:01:53.000Z (8 months ago)
- Last Synced: 2025-04-30T11:24:31.582Z (8 months ago)
- Topics: async, async-python, decorators, decorators-python, dependency-injection, fastapi, mypy, python, type-hints, typed
- Language: Python
- Homepage:
- Size: 796 KB
- Stars: 14
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# python-injection
[](https://github.com/100nm/python-injection)
[](https://pypi.org/project/python-injection)
[](https://github.com/astral-sh/ruff)
Fast and easy dependency injection framework.
## Installation
⚠️ _Requires Python 3.12 or higher_
```bash
pip install python-injection
```
## Motivations
1. Easy to use
2. No impact on class and function definitions
3. Easily interchangeable dependencies _(depending on the runtime environment, for example)_
4. No prerequisites
## Quick start
Simply apply the decorators and the package takes care of the rest.
```python
from injection import injectable, inject, singleton
@singleton
class Printer:
def __init__(self):
self.history = []
def print(self, message: str):
self.history.append(message)
print(message)
@injectable
class Service:
def __init__(self, printer: Printer):
self.printer = printer
def hello(self):
self.printer.print("Hello world!")
@inject
def main(service: Service):
service.hello()
if __name__ == "__main__":
main()
```
## Resources
> ⚠️ The package isn't threadsafe, for better performance in single-threaded applications and those using `asyncio`.
> So remember to use `threading.Lock` if you're writing a multithreaded program.
* [**Basic usage**](https://github.com/100nm/python-injection/tree/prod/documentation/basic-usage.md)
* [**Scoped dependencies**](https://github.com/100nm/python-injection/tree/prod/documentation/scoped-dependencies.md)
* [**Testing**](https://github.com/100nm/python-injection/tree/prod/documentation/testing.md)
* [**Advanced usage**](https://github.com/100nm/python-injection/tree/prod/documentation/advanced-usage.md)
* [**Utils**](https://github.com/100nm/python-injection/tree/prod/documentation/utils.md)
* [**Integrations**](https://github.com/100nm/python-injection/tree/prod/documentation/integrations.md)
* [**Concrete example**](https://github.com/100nm/python-injection-example)