Monadic Context
A lightweight, type-safe dependency injection library for Python.
Features
- Type-safe dependency injection with full Python type hints support
- Zero runtime dependencies - just pure Python
- Monadic interface for composition and transformation
- Pythonic generator-based syntax for requesting dependencies
- Flexible context creation with multiple builder patterns
Installation
Requires Python 3.9+
pip install monadic-context
Or with Poetry:
poetry add monadic-context
Quick Example
from monadic_context import requires, use
import monadic_context as context
# Define tags for your dependencies
port_tag = context.Tag[int]("port")
host_tag = context.Tag[str]("host")
# Function that requires dependencies from context
@requires
def build_url():
port = yield from use(port_tag)
host = yield from use(host_tag)
return f"http://{host}:{port}"
# Create a context with required dependencies
ctx = context.from_dict({port_tag: 8080, host_tag: "localhost"})
# Run the function with the context
url = ctx.run(build_url())
print(url) # Output: http://localhost:8080
Context Creation
The library offers multiple ways to create contexts:
# Single dependency
ctx1 = context.of(port_tag)(8080)
# Joining contexts
ctx2 = ctx1.join(context.of(host_tag)("localhost"))
# From pairs (more efficient for multiple dependencies)
ctx3 = context.from_pairs(
(port_tag, 8080),
(host_tag, "localhost"),
)
# From dictionary
ctx4 = context.from_dict({port_tag: 8080, host_tag: "localhost"})
Advanced Usage
Monadic Operations
The library supports standard monadic operations:
# Map over a context-requiring function
home_url = context.pipe(build_url(), context.map(lambda url: f"{url}/home"))
result = ctx.run(home_url)
print(result) # Output: http://localhost:8080/home
With Service
For functions that take a service as first argument:
import socket
db_conn_tag = context.Tag[socket.SocketType]("db_conn")
@context.with_service(db_conn_tag)
def configure_server(db_conn: socket.SocketType, timeout=30):
# Use db_conn to configure server
return {"connection": db_conn, "timeout": timeout}
Why Use Monadic Context?
- Testability: Easy to mock dependencies for testing
- Composability: Combine and transform context-aware functions
- Type Safety: Full type checking with mypy/pyright
- Separation of Concerns: Clean separation between business logic and dependency resolution
- No Runtime Reflection: Unlike some DI frameworks, no runtime reflection or complex containers
- No Mypy Plugins: No need to reconfigure your programming environment
License
MIT
Metadata
Release files for monadic-context 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| monadic_context-0.2.2.tar.gz | 5.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| monadic_context-0.2.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.9 kB
Release files / monadic_context-0.2.2.tar.gz
| Download URL | monadic_context-0.2.2.tar.gz |
|---|---|
| Size | 5.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3a82d3ebf336daca13ca4aed360e24a79a5e0c0bffe718d92e9c23809d78d4fe
|
|
BLAKE2b-256 checksum How to use checksums |
5df47f62cc20979e31d5e4e57327862e2c79e187dd049ad2e0d965c25148c73f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.0 CPython/3.12.10 Linux/6.12.21-gentoo-dist
|
Release files / monadic_context-0.2.2-py3-none-any.whl
| Download URL | monadic_context-0.2.2-py3-none-any.whl |
|---|---|
| Size | 7.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e9e2878616bda63a11b264e6e61f4f813473a0ac2550d25a00dad9485903fb4b
|
|
BLAKE2b-256 checksum How to use checksums |
b0e4e699b26dbfaea22732af8bfb5640b867c88d82cdbd1cceeddb4e4ddbe599
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.0 CPython/3.12.10 Linux/6.12.21-gentoo-dist
|