Skip to main content

A lightweight, type-safe dependency injection library

Project description

Monadic Context

PyPI version Python versions License

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

monadic_context-0.2.2.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

monadic_context-0.2.2-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file monadic_context-0.2.2.tar.gz.

File metadata

  • Download URL: monadic_context-0.2.2.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.0 CPython/3.12.10 Linux/6.12.21-gentoo-dist

File hashes

Hashes for monadic_context-0.2.2.tar.gz
Algorithm Hash digest
SHA256 3a82d3ebf336daca13ca4aed360e24a79a5e0c0bffe718d92e9c23809d78d4fe
MD5 74ed6451398caf8c9949cb14ba1175d6
BLAKE2b-256 5df47f62cc20979e31d5e4e57327862e2c79e187dd049ad2e0d965c25148c73f

See more details on using hashes here.

File details

Details for the file monadic_context-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: monadic_context-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.0 CPython/3.12.10 Linux/6.12.21-gentoo-dist

File hashes

Hashes for monadic_context-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e9e2878616bda63a11b264e6e61f4f813473a0ac2550d25a00dad9485903fb4b
MD5 cee0e50b1173ff040ce37ccfcf37ef81
BLAKE2b-256 b0e4e699b26dbfaea22732af8bfb5640b867c88d82cdbd1cceeddb4e4ddbe599

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page