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.3.tar.gz (44.2 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.3-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mountaineer_di-0.1.3.tar.gz
  • Upload date:
  • Size: 44.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mountaineer_di-0.1.3.tar.gz
Algorithm Hash digest
SHA256 1fad18638dade1a4b9d60a0fe0d52892aba44b1e056c282304a5ab4cc13c4c00
MD5 a0d38af064e0cfe65f40015d0ecb8ef0
BLAKE2b-256 003b9682b70eabf35b0297f2d04d99f778b7ee6a3fd1cbc5568d4c5fcc4d6275

See more details on using hashes here.

Provenance

The following attestation bundles were made for mountaineer_di-0.1.3.tar.gz:

Publisher: ci.yml on piercefreeman/mountaineer-di

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: mountaineer_di-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mountaineer_di-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3bf7cae8adaf2460cc88f907db050cdcee4d0b6de81f0b0cfad930585e157889
MD5 694d6ce14b1c291dac5cc7eb32dd63f6
BLAKE2b-256 b31ecab4d03fb9cf4bb3028d64e27d6a210d78b30c086e319662b74be33381f6

See more details on using hashes here.

Provenance

The following attestation bundles were made for mountaineer_di-0.1.3-py3-none-any.whl:

Publisher: ci.yml on piercefreeman/mountaineer-di

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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