Attodi
Minimalistic DI library for python
Attodi is a small dependency-injection container for Python. You register
types (and optionally concrete implementations or pre-built instances) on a
ServiceCollection, then resolve fully-constructed instances from a
ServiceProvider, which builds constructor arguments automatically from
type hints.
Requirements
- Python >= 3.12
Installation
pip install attodi
Usage
from attodi.core import ServiceCollection, ServiceProvider
class Greeter:
def __init__(self, name: str):
self.name = name
def greet(self) -> str:
return f"Hello, {self.name}!"
services = ServiceCollection()
services.add_singleton(str, service="World")
services.add_singleton(Greeter)
provider = ServiceProvider(services)
greeter = provider.get_service(Greeter)
print(greeter.greet()) # Hello, World!
ServiceProvider inspects the constructor of the class being resolved,
matches its parameter type hints against registered services, resolves them
recursively, and instantiates the class with the results.
Service lifetimes
Register services with one of three lifetimes:
| Method | Lifetime | Behavior |
|---|---|---|
add_singleton(cls, concrete=None, service=None) |
Singleton |
One shared instance per ServiceProvider (or a pre-built service value). |
add_scoped(cls, concrete=None) |
Scoped |
One shared instance per scope (see create_scope). |
add_transient(cls, concrete=None) |
Transient |
A new instance every time it is resolved. |
services = ServiceCollection()
services.add_scoped(Database)
provider = ServiceProvider(services)
scope = provider.create_scope()
db = scope.get_service(Database)
Resolving multiple registrations
get_service returns the first matching registration; get_services
returns an iterator over every registration that matches a type (including
subclasses).
for service in provider.get_services(Person):
...
License
GPL-3.0
Metadata
Release files for attodi 0.0.10
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 | |
|---|---|---|---|---|
| attodi-0.0.10.tar.gz | 18.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| attodi-0.0.10-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 34.1 kB
Release files / attodi-0.0.10.tar.gz
| Download URL | attodi-0.0.10.tar.gz |
|---|---|
| Size | 18.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1e55566b275eb5a11399abe15f3421a92689bd31cb689fd58e6e032fc2b5cc36
|
|
BLAKE2b-256 checksum How to use checksums |
976e1abd346da2f4bdf039c0dfff74ab270e44464f21618082a85012cd4dc128
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|
Release files / attodi-0.0.10-py3-none-any.whl
| Download URL | attodi-0.0.10-py3-none-any.whl |
|---|---|
| Size | 15.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9eaf53c665bca7cdca727260bfeafd164298b525500e5742832d671c5cd392b7
|
|
BLAKE2b-256 checksum How to use checksums |
6261233c25788895082d6b240a166d9b6985c27bf08627970d4bf499e6d78239
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|
Security for attodi 0.0.10
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.