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

Uploaded Python 3

File details

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

File metadata

  • Download URL: mountaineer_di-0.1.1.tar.gz
  • Upload date:
  • Size: 30.4 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.1.tar.gz
Algorithm Hash digest
SHA256 bca705b8bec23ff7e786e83fdfcd7200b52593562ab488abfe94e6a28c6eae68
MD5 270cfd313a01be7f40001c79d49e969e
BLAKE2b-256 d4fec90964eca28fa840f26cafcf067f6397f938fdf6996445a3d53a78e85104

See more details on using hashes here.

Provenance

The following attestation bundles were made for mountaineer_di-0.1.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: mountaineer_di-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.2 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7d34d65091baa9d802bc8caf94d393e1107e69fa4bec6d38c035d4c9b6ec8b81
MD5 d752e63ae13ae27cf5842b956acb4c64
BLAKE2b-256 97949b2b641de8d9efbe2e03962ba15cd0587b69ae112f02e33ca72386a29846

See more details on using hashes here.

Provenance

The following attestation bundles were made for mountaineer_di-0.1.1-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