Registry / testing / simple-di

simple-di

JSON →
library0.1.5pypypi✓ verified 83d ago

Simple-DI is a lightweight Python library for dependency injection. It helps manage application components and their dependencies in a clear, testable, and maintainable way using a container pattern. As of version 0.1.5, it provides core features like `Container` for organizing providers and `Provider` for defining how dependencies are created. Releases are infrequent, driven by new features or bug fixes.

pip install simple-di
INSTALL
IMPORT
SIG · SIMPLE-DI
S
simple-di
testingpythonv0.1.5
Install
1.5s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1.5 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.5s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

container
from simple_di import container
from simple_di import Container
inject
from simple_di import inject
Provide
from simple_di import Provide

This quickstart demonstrates how to define a `Container` for configuration, nest containers, define `Provider` instances with dependencies, resolve dependencies using `.get()`, and dynamically override configuration values.

from simple_di import Container, Provider class MyConfig(Container): debug_mode = Provider(bool, default=False) database_url = Provider(str, default="sqlite:///test.db") class MyDependencies(Container): # Instantiate nested containers config = MyConfig() # A factory function for a database client def _create_db_client(url: str): print(f"[DB] Connecting to {url}...") return f"DatabaseClient({url})" # Define a provider for the database client, depending on config.database_url db_client = Provider(_create_db_client, config.database_url) # A factory function for the main application def _create_app(client): print(f"[App] Creating app with client: {client}") return f"MyApp({client})" # Define a provider for the app, depending on db_client app = Provider(_create_app, db_client) # Instantiate the main dependency container my_app_deps = MyDependencies() # Accessing dependencies (calling .get() is crucial to resolve them) print("\n--- First Resolution ---") db_client_instance = my_app_deps.db_client.get() print(f"Retrieved DB Client: {db_client_instance}") app_instance = my_app_deps.app.get() print(f"Retrieved App: {app_instance}") # Overriding a configuration value and observing changes print("\n--- Overriding Configuration ---") my_app_deps.config.database_url.set("postgresql://user:pass@host/prod_db") # Accessing again will re-evaluate the db_client provider due to dependency change print("\n--- Second Resolution (after override) ---") new_db_client_instance = my_app_deps.db_client.get() print(f"Retrieved NEW DB Client: {new_db_client_instance}") # Accessing app again will use the new db client new_app_instance = my_app_deps.app.get() print(f"Retrieved NEW App: {new_app_instance}")
Debug
Known issues
breakingPrior to version 0.1.0, `Container` classes could be used directly to access providers (e.g., `MyContainer.my_provider`). From 0.1.0 onwards, `Container` classes must be instantiated (e.g., `my_container = MyContainer()`) before their providers can be accessed.
fix
Change direct class access like `MyContainer.my_provider` to `MyContainer().my_provider` or, preferably, instantiate the container once: `my_container_instance = MyContainer()` and then use `my_container_instance.my_provider`.
affects: >=0.1.0
gotchaVersions prior to 0.1.2 did not ship with type information, potentially leading to issues with IDE auto-completion, static analysis, and type checkers like MyPy.
fix
Upgrade to `simple-di>=0.1.2` to ensure type hints are available for a better developer experience and more robust code.
affects: <0.1.2
gotchaAccessing a `Provider` attribute directly (e.g., `my_deps.db_client`) returns the `Provider` object itself, not the resolved dependency. To obtain the actual value (the object that the provider is configured to create), you must call `.get()` on the provider.
fix
Change `my_deps.db_client` to `my_deps.db_client.get()` to retrieve the resolved dependency value.
affects: All versions
gotchaCircular dependencies between `Provider` instances will lead to runtime errors (typically `RecursionError`) when the dependency graph is resolved. `simple-di` does not automatically detect or resolve these during setup.
fix
Refactor your application's dependency graph to eliminate circular references. Ensure that dependencies always flow in a single, acyclic direction.
affects: All versions
Upgrade
Version history
0.1.5latest on PyPI · released Feb 22, 2022
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
4
Resources
simple-di — pip install simple-di · libregistry