Skip to main content

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

Project description

inversipy

CI PyPI version Python versions License Coverage Docs

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.3.0.tar.gz (136.2 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.3.0-py3-none-any.whl (30.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for inversipy-0.3.0.tar.gz
Algorithm Hash digest
SHA256 c61fbda6cdb0e7037b963d0dc2c5c943e6971c135eac64300c05c258412c6790
MD5 c2513f6285f9e2f6b7e8500e199d2177
BLAKE2b-256 a56f5532f71a5e2979fcedcf0734a4aec6443eb27f6e8f950dc480159340b114

See more details on using hashes here.

Provenance

The following attestation bundles were made for inversipy-0.3.0.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.3.0-py3-none-any.whl.

File metadata

  • Download URL: inversipy-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 30.9 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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 10b2613346e2f0ebd9eb97e6b90116d3f72df6dda4b259797da2c367db0b60c4
MD5 1af64da5c94e6512184782f9bc769b52
BLAKE2b-256 393039fb72bc9696d6006c35c29c22496963bbdf9e13697678576932458d3ed3

See more details on using hashes here.

Provenance

The following attestation bundles were made for inversipy-0.3.0-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