A minimalist, opinionated dependency injection framework
Project description
TiDI
Intro
TiDI—short for Timber Dependency Injection—is a minimalist, opinionated dependency injection framework created by me, Timber. It emphasizes simplicity, ease of use, and a principled approach to DI.
In the spirit of real DI
The essence of dependency injection is to invert the direction of dependency: the client specifies what type of dependencies it requires, while someone else decides which concrete instances to provide. Many DI frameworks blur this distinction by introducing configuration logic directly into the client—who should only declare its dependencies, not manage them. While this can seem convenient, it undermines the core value of dependency injection.
TiDI maintains this separation strictly. Consider the following client:
class MyClient:
def __init__(self, dependency: MyDependency):
...
No TiDI-specific elements are required in MyClient. Instead, composition is handled externally in a dedicated composer function:
@composer
def my_client() -> MyClient:
return MyClient(MyDependency())
This keeps the client clean and focused and ensures that wiring and instantiation remain a separate responsibility of the composition root.
Keeping dependency composition explicitly separate is the core principle of DI—but it does come with the trade-off of writing a bit more code. TiDI aims to make defining the composition root as convenient as possible. One key feature that helps with this is auto composition:
auto_compose(MyClient)
This allows TiDI to automatically resolve and compose the dependencies in many cases, reducing boilerplate while preserving separation of concerns.
Scopes and lifetime management
Composition isn't just about specifying how dependencies are created and connected—it's also about managing when they are instantiated and cleaned up. Not every object in the dependency graph needs to be created upfront, and clients should stay unaware of the timing or lifecycle of their dependencies.
TiDI handles this through scopes. Every composer in TiDI is associated with a scope type—by default, this is root. You can assign a custom scope like this:
@composer(scope_type='request')
def my_dependency() -> MyDependency:
...
This means MyDependency will only be available within scopes of type request. To resolve it during a specific request, you'd do:
ensure_scope(scope_id='my-request', scope_type='request')
resolver = get_resolver('my-request')
resolver(MyDependency)
When the request ends, the scope—and any dependencies created within it—can be explicitly cleared:
clear_scope('my-request')
This ensures that scoped dependencies are properly cleaned up, giving you fine-grained control over object lifetimes.
A tree of scopes
Each scope in the system has a parent scope. If no parent is explicitly defined, the scope defaults to having root as its parent. Scopes form a hierarchy, and any scope can access the dependencies registered in its own context as well as those of all its ancestors.
Here's how you can define dependencies in different scopes:
auto_compose(DependencyA) # Registered in the root scope
auto_compose(DependencyB, scope_type='tenant') # Registered in tenant-level scope
auto_compose(DependencyC, scope_type='request') # Registered in request-level scope
Now we can start a request scope within a specific tenant scope:
ensure_scope(scope_id='my-tenant', scope_type='tenant') # Create tenant scope
ensure_scope(scope_id='my-request', scope_type='request', parent_id='my-tenant') # Create request scope with tenant as parent
resolver = get_resolver('my-request') # Get a resolver for the request scope
Once the scopes are in place, you can resolve dependencies from the request scope. The resolver will automatically traverse up the scope hierarchy as needed:
resolver(DependencyA) # Resolved from root scope
resolver(DependencyB) # Resolved from tenant scope
resolver(DependencyC) # Resolved from request scope
Working with scope context
A common scenario in dependency injection is when a client depends on an interface that has multiple implementations. In such cases, the composition logic must decide which concrete implementation to provide.
class Repository(ABC):
@abstractmethod
get_by_id(id: str):
...
class InMemoryRepository(Repository):
...
class PostgresRepository(Repository):
...
If both InMemoryRepository and PostgresRepository have their own composers, then the type Repository becomes ambiguous—TiDI won’t know which one to resolve by default.
You can use scope context — key-value tags that express context-specific choices.
@composer(stage='prod')
def postgres_repository() -> PostgresRepository:
return PostgresRepository()
@composer(stage='test')
def in_memory_repository() -> InMemoryRepository:
return InMemoryRepository()
The line @composer(stage='prod') means that if the key stage is part of the scope context, this composer will only
be considered if it has value prod.
We add context to our scope with:
add_context(stage='test')
This now means that resolve(Repository) will resolve to an InMemoryRepository.
Using an identifier
Sometimes, you need multiple composers that return the same type but represent different roles or configurations. By assigning an explicit id to each composer, you can resolve the exact one you need without ambiguity.
@composer(id='primary-db')
def primary_connection() -> DatabaseConnection:
return DatabaseConnection(dsn='postgresql://primary.db.local')
@composer(id='analytics-db')
def analytics_connection() -> DatabaseConnection:
return DatabaseConnection(dsn='postgresql://analytics.db.local')
Here, both composers return DatabaseConnection, but they're configured for different purposes. Resolving DatabaseConnection is ambiguous.
Instead, you can resolve them explicitly by ID:
resolve(DatabaseConnection, id='primary-db')
Since IDs are unique, this guarantees an unambiguous resolution.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tidipy-0.1.0.tar.gz.
File metadata
- Download URL: tidipy-0.1.0.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b7e61dba3863937b5064433bf5a3cd6adb627574985d689ac93af739e24dbda3
|
|
| MD5 |
c60e738c0bf81aa9555f7bbb1b8d1ee5
|
|
| BLAKE2b-256 |
672fb6c7a8ea840e9bcff81dbdf02a81d94ded8a72b959735b7584d2bd0af21c
|
File details
Details for the file tidipy-0.1.0-py3-none-any.whl.
File metadata
- Download URL: tidipy-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c87bd629c328f548a3483dc570f2264bd49a528932a4b342a9876fed848fd38b
|
|
| MD5 |
c55d52185f34fcbe41f7facfebd451b5
|
|
| BLAKE2b-256 |
1fde9431a76b0d6b668a0ce49fdae6b5e80e26b39fd4a695f8312f5109bcd11a
|