Skip to main content

Common dependency injection utilities for mountaineer & friends

Project description

mountaineer-di

Common dependency injection utilities for mountaineer & friends, with pydantic as the only dependency.

This package provides a small dependency resolver built around mountaineer_di.Depends(...). It works on its own, and when FastAPI is installed it can also interoperate with fastapi.Depends(...) inside the same dependency graph. You probably won't have to use this library explicitly but if it's helpful to you then you're certainly welcome to.

Getting Started

Install the package with uv:

uv add mountaineer-di

Native Usage

Use Depends(...) to declare dependencies, then resolve a callable with provide_dependencies(...):

from typing import Annotated

from mountaineer_di import Depends, provide_dependencies


def get_prefix() -> str:
    return "hello"


def get_message(prefix: str = Depends(get_prefix)) -> str:
    return f"{prefix} world"


async def handler(message: Annotated[str, Depends(get_message)]) -> str:
    return message


async with provide_dependencies(handler) as kwargs:
    result = await handler(**kwargs)
print(result)  # hello world

provide_dependencies(...) keeps generator and context-manager dependencies alive for the duration of the async context.

FastAPI Interop

If you're already using FastAPI elsewhere in your code, you can also use mountaineer-di to call into dependencies that use fastapi.Depends(...):

from fastapi import Depends as FastAPIDepends, Request

from mountaineer_di import Depends, get_function_dependencies


def get_user_agent(request: Request) -> str | None:
    return request.headers.get("user-agent")


def get_context(
    user_agent: str | None = FastAPIDepends(get_user_agent),
) -> str:
    return user_agent or "unknown"


async def task(context: str = Depends(get_context)) -> str:
    return context

For request-bound resolution, pass the request object and route template into get_function_dependencies(...) or provide_dependencies(...).

Development

Development commands are available through the repo Makefile, with lint, ci-lint, lint-ruff, lint-ty, and test targets following the same pattern as sibling Mountaineer repositories.

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

mountaineer_di-0.1.0.tar.gz (29.4 kB view details)

Uploaded Source

Built Distribution

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

mountaineer_di-0.1.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file mountaineer_di-0.1.0.tar.gz.

File metadata

  • Download URL: mountaineer_di-0.1.0.tar.gz
  • Upload date:
  • Size: 29.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.13

File hashes

Hashes for mountaineer_di-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ad2e14c08b853eac2e26b1a4a6745adc729d534029b4cce1178e96102a7129a1
MD5 ce3fb074b9b73a48be45d74af27dac8d
BLAKE2b-256 1285c2916fc2f76cc579038ab52b999e792d7fcb5a5dc7b3aba0f3270a1427d5

See more details on using hashes here.

File details

Details for the file mountaineer_di-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mountaineer_di-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 529e26b7887d8634216e7e79f523b725f8151821d94dd729c4f2ec954c0ddde8
MD5 d30d3ca59b4deb5cb29ebb367e0ff746
BLAKE2b-256 0048ad565bc77d861df005de5ccdd05505c80caf82acdbd11c8efbc6e3354fc4

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