
Security News
TC39 Advances 11 Proposals for Math Precision, Binary APIs, and More
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
DuckDI is a minimal, type-safe, and architecture-friendly dependency injection library for Python.
It provides a clean interface to register and resolve dependencies at runtime using a TOML-based configuration, following the duck typing principle: "if it implements the expected methods, itβs good enough."
Ideal for developers who want clarity, zero magic, and full control over dependency resolution.
ABC
and regular classes (no need for Protocols)INJECTIONS_PATH
)With Poetry:
poetry add duckdi
Or using pip:
pip install duckdi
from duckdi import Interface
from abc import ABC, abstractmethod
@Interface
class IUserRepository(ABC):
@abstractmethod
def get_user(self, user_id: str) -> dict: ...
from duckdi import register
class PostgresUserRepository(IUserRepository):
def get_user(self, user_id: str) -> dict:
return {"id": user_id, "name": "John Doe"}
register(PostgresUserRepository)
You can also register it as a singleton:
register(PostgresUserRepository, is_singleton=True)
Create a file called injections.toml
:
[injections]
"i_user_repository" = "postgres_user_repository"
Set the injection file path using the INJECTIONS_PATH
environment variable:
export INJECTIONS_PATH=./injections.toml
from duckdi import Get
repo = Get(IUserRepository)
user = repo.get_user("123")
print(user) # {'id': '123', 'name': 'John Doe'}
MissingInjectionPayloadError
Raised when no injection payload file is found at the specified path.
InvalidAdapterImplementationError
Raised when the adapter registered does not implement the expected interface.
InterfaceAlreadyRegisteredError
Raised when trying to register the same interface twice.
AdapterAlreadyRegisteredError
Raised when the same adapter is registered more than once.
duckdi/
βββ pyproject.toml
βββ README.md
βββ src/
β βββ duckdi/
β βββ __init__.py
β βββ cli.py
β βββ duck.py
β βββ errors/
β β βββ __init__.py
β β βββ invalid_adapter_implementation_error.py
β β βββ interface_already_registered_error.py
β β βββ adapter_already_registered_error.py
β β βββ missing_injection_payload_error.py
β βββ utils/
β β βββ __init__.py
β β βββ buffer_readers.py
β β βββ to_snake.py
β βββ injections/
β βββ injections_container.py
β βββ injections_payload.py
βββ tests/
βββ test_interface.py
βββ test_register.py
βββ test_get.py
You can register multiple adapters and resolve them dynamically based on the TOML mapping:
from duckdi import Interface, register, Get
@Interface
class INotifier:
def send(self, msg: str): ...
class EmailNotifier(INotifier):
def send(self, msg: str):
print(f"Sending email: {msg}")
register(EmailNotifier)
# injections.toml
# [injections]
# "i_notifier" = "email_notifier"
notifier = Get(INotifier)
notifier.send("Hello from DuckDI!")
To run tests:
pytest
Or via Makefile:
make test
To check static typing:
make check
Licensed under the MIT License.
See the LICENSE file for more information.
Made with β€οΈ by PhePato
Pull requests, issues and ideas are always welcome!
FAQs
π¦ A minimal dependency injection library for Python
We found that duckdi demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.Β It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
Research
/Security News
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
Product
Customize license detection with Socketβs new license overlays: gain control, reduce noise, and handle edge cases with precision.