FastInject: easy Python dependency injection
FastInject provides easy dependency injection for Python that makes you code decoupled, testable, uncomplicated and more readable.
Decorate your services with the @injectable decorator and decorate your function with @inject. Done!
Your function will now be injected with instances of the required service.
pip install fastinject
Table of Contents
- Main Features
- Usage Example
- Installation
- Dependencies
- License
- Documentation
- Development
- Contributing to Cachr
Main Features
- 🐍 Pure Python
- 🤸 Flexible
- 🎩 Tailor-made for your app
- 👨🎨 Easy to use with decorators
How to
Injecting services
Inject services that depend on one another
- ServiceConfig: Register multiple dependencies for injection.
- ServiceConfig: Register nested dependencies for injection.
Use the service registy imperatively to get and set dependencies on the fly
- Declare service to be injectable imperatively.
- Declare service to be injectable and declare function to inject imperatively.
Use multiple registries?
Register similar services?
Usage Example
Below details a
Step 1: Declare service to be injectable
We have a service that we want to inject, so we mark it injectable with a decorator:
import time, datetime
from fastinject import injectable
@injectable() # <-- Just add this decorator to declare the TimeStamp service to be injectable
class TimeStamp:
ts: float
def __init__(self) -> None:
self.ts = time.time()
@property
def datetime_str(self) -> str:
return datetime.datetime.fromtimestamp(self.ts).strftime("%Y-%m-%d %H:%M:%S")
Step 2: Use the service in a function that is injected in
from fastinject import inject
@inject() # <-- This decorator will inject required services in this function
def function_with_injection(ts: TimeStamp):
print(f"In the injected function, the current time is {ts.datetime_str}.")
if __name__ == "__main__":
function_with_injection()
Installation
pip install fastinject
The source code is currently hosted on GitHub at: https://github.com/mike-huls/fastinject
Binary installers for the latest released version are available at the Python Package Index (PyPI).
Dependencies
FastInject has one major dependency: injector. FastInject aims to build on injector by making it easier to use.
License
Documentation
🔨 Under construction
Development
Find the changelog and list of upcoming features here.
Contributions are always welcome; feel free to submit bug reports, bug fixes, feature requests, documentation improvements or enhancements!
Metadata
Release files for fastinject 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| fastinject-0.0.4.tar.gz | 14.2 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastinject-0.0.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 22.7 kB
Release files / fastinject-0.0.4.tar.gz
| Download URL | fastinject-0.0.4.tar.gz |
|---|---|
| Size | 14.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e041fc9304c1fd10344a7afd351d6af8cc117193c46102b75184d0d7f454ea49
|
|
BLAKE2b-256 checksum How to use checksums |
1fdab4a2c0e1be8694ee9cf418e69780c809f3d8352ccff064725cfb2a74ad53
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.5.1
|
Release files / fastinject-0.0.4-py3-none-any.whl
| Download URL | fastinject-0.0.4-py3-none-any.whl |
|---|---|
| Size | 8.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0c54b25acd3407bb1dd548ee275f3ded6cbee2698e5078e0e0a483f61524d936
|
|
BLAKE2b-256 checksum How to use checksums |
3f5d300de350886b6bdcba0f9baee4efe61e65febb48f648c238f1e1762f89ca
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.5.1
|
Security for fastinject 0.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.