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.12+

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.1.tar.gz (5.3 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.1-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: monadic_context-0.2.1.tar.gz
  • Upload date:
  • Size: 5.3 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.1.tar.gz
Algorithm Hash digest
SHA256 f76b8b4cf8337e64c3b98c00a8acfbd8a862e22e93ea376883f1c2637b1a90c4
MD5 98cfeaeebb2aa3424febd8d2e98a253d
BLAKE2b-256 67a2dfda74dcba6e81489e83a18b17d1ba4c5a8662e87c06b6b89dac2a48e9f6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: monadic_context-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 7.1 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2a28411a20794026ed590f1398a1ac657eb95024711ec35a9c82e2e77d9217d6
MD5 6e88e24afa504bb77dfdc840fbc10d99
BLAKE2b-256 551ceade3363b272579f4ea8b7b8eb41e1bae9259e0c7c36a67ed39e9f133407

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