Skip to main content

Tiny, typed dependency injection built on FastDepends.

Project description

wireme

wireme - tiny, typed dependency injection for Python

Tiny, typed dependency injection for Python, powered by FastDepends.

Wireme keeps dependency injection explicit and small:

from wireme import Wired, override_dependency, wire, wired
  • @wire enables dependency resolution for a class constructor, function, or method.
  • wired(factory) declares how a dependency is created.
  • Wired() marks a reusable Annotated dependency as caller-optional.
  • override_dependency() temporarily replaces a dependency in tests.

Installation

uv add wireme

With the optional FastAPI integration:

uv add 'wireme[fastapi]'

Wireme requires Python 3.12 or newer.

Sixty-second tour

Declare a dependency once, inject it anywhere. Injected parameters go after * and disappear from the public signature:

from typing import Annotated

from wireme import Wired, wire, wired


class Database:
    def write(self, value: str) -> None:
        print(f"writing: {value}")


def get_database() -> Database:
    return Database()


type DatabaseDep = Annotated[Database, wired(get_database)]


@wire
def create_user(username: str, *, database: DatabaseDep = Wired()) -> None:
    database.write(username)


create_user("mo")  # database is resolved automatically

Classes wire their constructors; tests swap factories:

from wireme import override_dependency


@wire
class UserService:
    def __init__(self, *, database: DatabaseDep = Wired()) -> None:
        self._database = database

    def list_users(self) -> list[str]:
        return []


def get_test_database() -> Database:
    return Database()


with override_dependency(get_database, get_test_database):
    UserService()  # built against the test database

With FastAPI, the same wired class plugs into endpoints while FastAPI keeps ownership of the request lifecycle:

from fastapi import FastAPI

from wireme.fastapi import FromWeb


app = FastAPI()


@app.get("/users")
def list_users(service: FromWeb[UserService]) -> list[str]:
    return service.list_users()

Documentation

Rendered documentation lives at wireme.mghalix.com, including recipes and the house style guide. The same pages are browsable in the repository; the guide covers one concept per page, in reading order:

Page What it covers
Getting started Install, wire a function, reusable dependencies
Wiring classes Constructor and method injection
The dependency graph Nesting, per-call caching, singletons
Resources Async factories and generator cleanup
Testing Overrides and explicit values
Side-effect dependencies Guards and auditing with requires
Validation control cast flags and error types
Protocol dependencies Depending on interfaces
FastAPI integration FromWeb, request-scoped resources, web overrides
Building integrations Wireme as your project's DI primitive

Every capability also has a small runnable example; see the example index.

Public API

Name Purpose
wire Decorate a class, function, or method and enable dependency resolution. Accepts cast, cast_result, and requires.
wired Declare a dependency factory and its resolution options.
Wired Mark an Annotated dependency as caller-optional.
override_dependency Temporarily replace a factory, with nested restoration.
WiremeError Base error exposed by Wireme.
ValidationError Dependency validation error.

The optional wireme.fastapi integration adds:

Name Purpose
FromWeb Annotate endpoint parameters with classes or wired aliases.
override_web_dependency Temporarily replace a web dependency, with nested restoration.

Why Wireme instead of importing FastDepends directly?

FastDepends provides the resolution engine. Wireme provides a deliberately small, opinionated facade with:

  • A cohesive wire, wired, and Wired vocabulary.
  • Strong return typing for sync, async, generator, and async-generator factories.
  • Reusable Annotated dependencies.
  • Injected parameters hidden from public runtime signatures.
  • Nested-safe dependency overrides.
  • A minimal backend-independent public namespace.

Versioning

Wireme follows SemVer with the strict 0.x mapping: below 1.0.0, a breaking change bumps minor and features or fixes bump patch, so the minor number is the breaking-change signal. Published artifacts are immutable; a broken release is fixed by publishing a new version.

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

wireme-0.1.1.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

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

wireme-0.1.1-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: wireme-0.1.1.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for wireme-0.1.1.tar.gz
Algorithm Hash digest
SHA256 df0e7941995dd610b43c8ad422f4690ae77f05da018c95d78323b4d262b0297e
MD5 4d778eccface0cdf7db2027819180556
BLAKE2b-256 b20f2543e859b4e234060a69f3ec28caf977e4cf46640c4fa63a6e5ae7076559

See more details on using hashes here.

File details

Details for the file wireme-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: wireme-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for wireme-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 69c939a5f8feeca93a615c2bd90c207ed00fbf55d44fc70dcabb863996c98302
MD5 daf1f0b59fdf2ae49d3872cccc5b6ec4
BLAKE2b-256 02b017be4e1412128333864ec5899c0f6b0977e9f0e5c4856c92b0aec00a1891

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