Skip to main content

A minimalist, zero-dependency Inversion of Control (IoC) container for Python.

Project description

📦 Pico-IoC: A Robust, Async-Native IoC Container for Python

PyPI Ask DeepWiki License: MIT CI (tox matrix) codecov Quality Gate Status Duplicated Lines (%) Maintainability Rating

Pico-IoC is a lightweight, async-ready, decorator-driven IoC container built for clarity, testability, and performance. It brings Inversion of Control and dependency injection to Python in a deterministic, modern, and framework-agnostic way.

🐍 Requires Python 3.10+


⚖️ Core Principles

  • Single Purpose – Do one thing: dependency management.
  • Declarative – Use simple decorators (@component, @factory, @configuration) instead of config files or YAML magic.
  • Deterministic – No hidden scanning or side-effects; everything flows from an explicit init().
  • Async-Native – Fully supports async providers, async lifecycle hooks, and async interceptors.
  • Fail-Fast – Detects missing bindings and circular dependencies at bootstrap.
  • Testable by Design – Use overrides and profiles to swap components instantly.
  • Zero Core Dependencies – Built entirely on the Python standard library. Optional features may require external packages (see Installation).

🚀 Why Pico-IoC?

As Python systems evolve, wiring dependencies by hand becomes fragile and unmaintainable. Pico-IoC eliminates that friction by letting you declare how components relate — not how they’re created.

Feature Manual Wiring With Pico-IoC
Object creation svc = Service(Repo(Config())) svc = container.get(Service)
Replacing deps Monkey-patch overrides={Repo: FakeRepo()}
Coupling Tight Loose
Testing Painful Instant
Async support Manual Built-in

🧩 Highlights (v2.0.0)

  • Full redesign: unified architecture with simpler, more powerful APIs.
  • Async-aware AOP system — method interceptors via @intercepted_by.
  • Typed configuration — dataclasses with JSON/YAML/env sources.
  • Scoped resolution — singleton, prototype, request, session, transaction.
  • UnifiedComponentProxy — transparent lazy/AOP proxy supporting serialization.
  • Tree-based configuration runtime with reusable adapters and discriminators.
  • Observable container context with stats, health checks, and async cleanup.

📦 Installation

pip install pico-ioc

For optional features, you can install extras:

  • YAML Configuration:

    pip install pico-ioc[yaml]
    

    (Requires PyYAML)

  • Dependency Graph Export:

    pip install pico-ioc[graphviz]
    

    (Requires the graphviz Python package and the Graphviz command-line tools)


⚙️ Quick Example

from dataclasses import dataclass
from pico_ioc import component, configuration, init

@configuration
@dataclass
class Config:
    db_url: str = "sqlite:///demo.db"

@component
class Repo:
    def __init__(self, cfg: Config):
        self.cfg = cfg
    def fetch(self):
        return f"fetching from {self.cfg.db_url}"

@component
class Service:
    def __init__(self, repo: Repo):
        self.repo = repo
    def run(self):
        return self.repo.fetch()

container = init(modules=[__name__])
svc = container.get(Service)
print(svc.run())

Output:

fetching from sqlite:///demo.db

🧪 Testing with Overrides

class FakeRepo:
    def fetch(self): return "fake-data"

container = init(modules=[__name__], overrides={Repo: FakeRepo()})
svc = container.get(Service)
assert svc.run() == "fake-data"

🩺 Lifecycle & AOP

from pico_ioc import intercepted_by, MethodInterceptor, MethodCtx

class LogInterceptor(MethodInterceptor):
    def invoke(self, ctx: MethodCtx, call_next):
        print(f"→ calling {ctx.name}")
        res = call_next(ctx)
        print(f"← {ctx.name} done")
        return res

@component
class Demo:
    @intercepted_by(LogInterceptor)
    def work(self):
        return "ok"

c = init(modules=[__name__])
c.get(Demo).work()

📖 Documentation

The full documentation is available within the docs/ directory of the project repository. Start with docs/README.md for navigation.

  • Getting Started: docs/getting-started.md
  • User Guide: docs/user-guide/README.md
  • Advanced Features: docs/advanced-features/README.md
  • Observability: docs/observability/README.md
  • Integrations: docs/integrations/README.md
  • Cookbook (Patterns): docs/cookbook/README.md
  • Architecture: docs/architecture/README.md
  • API Reference: docs/api-reference/README.md
  • ADR Index: docs/adr/README.md

🧩 Development

pip install tox
tox

🧾 Changelog

See CHANGELOG.mdFull redesign for v2.0.0.


📜 License

MIT — LICENSE

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

pico_ioc-2.0.1.tar.gz (150.7 kB view details)

Uploaded Source

Built Distribution

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

pico_ioc-2.0.1-py3-none-any.whl (32.6 kB view details)

Uploaded Python 3

File details

Details for the file pico_ioc-2.0.1.tar.gz.

File metadata

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

File hashes

Hashes for pico_ioc-2.0.1.tar.gz
Algorithm Hash digest
SHA256 09774a4d5988f92ae45f8be438e15eca2748cfe0271f0e21153d7cb78eda51cc
MD5 59fa601060aff9a705a66267f76114d0
BLAKE2b-256 a686fe054110c681b96b7553d1ebc167876ba93981db56892c290e8677540701

See more details on using hashes here.

File details

Details for the file pico_ioc-2.0.1-py3-none-any.whl.

File metadata

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

File hashes

Hashes for pico_ioc-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3b14279b2b125137f966a719afb1403537194353ddcf9662069fff9817e41fb2
MD5 c570fed3ad50588108d856da2c0ec181
BLAKE2b-256 35dd8e7d53129a8a688e1d2952a2f159e6fe1506edfff810c03978a614788a78

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