Skip to main content

A powerful and type-safe dependency injection/IoC library for Python

Project description

inversipy

A powerful and type-safe dependency injection/IoC (Inversion of Control) library for Python 3.12+.

Features

  • Type annotation-based - Dependencies resolved using Python type hints
  • Pure classes - No container coupling; classes remain framework-agnostic
  • Zero runtime dependencies - Pure Python
  • Async-first - First-class support for async dependencies and factories
  • Module system - Organize dependencies with public/private access control
  • Multiple scopes - Singleton, Transient, and Request
  • Named dependencies - Multiple implementations with named disambiguation
  • Collection injection - Register and inject multiple implementations with InjectAll
  • Optional dependencies - T | None parameters resolve to None when unregistered
  • Container freezing - Lock the container after configuration
  • Validation - Catch configuration errors at startup
  • FastAPI integration - @inject decorator for route handlers
  • Full type safety - Strict MyPy support with py.typed marker

Installation

pip install inversipy

Quick Start

from inversipy import Container, Scopes

class Database:
    def query(self, sql: str) -> list:
        return ["result"]

class UserRepository:
    def __init__(self, db: Database) -> None:
        self.db = db

    def get_users(self) -> list:
        return self.db.query("SELECT * FROM users")

class UserService:
    def __init__(self, repo: UserRepository) -> None:
        self.repo = repo

    def list_users(self) -> list:
        return self.repo.get_users()

# Create container and register dependencies
container = Container()
container.register(Database, scope=Scopes.SINGLETON)
container.register(UserRepository)
container.register(UserService)

# Validate and freeze
container.validate()
container.freeze()

# Resolve dependencies
service = container.get(UserService)
users = service.list_users()

Documentation

Full documentation is available at the documentation site.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License - see LICENSE file for details.

Similar Projects

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

inversipy-0.2.1.tar.gz (23.8 kB view details)

Uploaded Source

Built Distribution

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

inversipy-0.2.1-py3-none-any.whl (29.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for inversipy-0.2.1.tar.gz
Algorithm Hash digest
SHA256 14e667817f0931d97f0fa77a3704114ec821f47287257c2529e04e9998619e2f
MD5 813c8021d9829774bbdccdcc95e93e7d
BLAKE2b-256 b43c21eb36462ad554c9e4027fb2f225c75dea5d6588566eeaae3f37df50c907

See more details on using hashes here.

Provenance

The following attestation bundles were made for inversipy-0.2.1.tar.gz:

Publisher: publish.yml on mottetm/inversipy

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

File details

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

File metadata

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

File hashes

Hashes for inversipy-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e750d3bc3775e8538d2b89e1cbf8f57509deb96c91927ae52bd4ee17bf2a927c
MD5 850ac434ea152dada827fab561570d83
BLAKE2b-256 2d63cbe4170c14cf13d49fce6a1511ffca38f0dc5f223c4fb5db2ac84ad26591

See more details on using hashes here.

Provenance

The following attestation bundles were made for inversipy-0.2.1-py3-none-any.whl:

Publisher: publish.yml on mottetm/inversipy

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