Skip to main content

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

Project description

📦 Pico-IoC: A Minimalist IoC Container for Python

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

pico-ioc is a tiny, zero-dependency, decorator-based IoC container for Python.
It helps you build loosely-coupled, testable apps without manual wiring. Inspired by the Spring ecosystem, but minimal.

⚠️ Requires Python 3.10+ (uses typing.Annotated and include_extras=True).


✨ Features

  • Zero dependencies — pure Python, framework-agnostic.
  • Decorator API@component, @factory_component, @provides, @plugin.
  • Fail-fast bootstrap — eager by default; missing deps surface at startup.
  • Opt-in lazylazy=True wraps with ComponentProxy.
  • Smart resolution order — parameter name → type annotation → MRO → string.
  • Qualifiers & collectionslist[Annotated[T, Q]] filters by qualifier.
  • Plugins — lifecycle hooks (before_scan, after_ready).
  • Public API helper — auto-export decorated symbols in __init__.py.
  • Thread/async safe — isolation via ContextVar.
  • Overrides for testing — inject mocks/fakes directly via init(overrides={...}).
  • Scoped subgraph for testsscope(modules=…, roots=…, overrides=…, strict=…, lazy=…, include_tags=…, exclude_tags=…) to load only what you need.

📦 Installation

# Requires Python 3.10+
pip install pico-ioc

🚀 Quick start

from pico_ioc import component, init

@component
class Config:
    url = "sqlite:///demo.db"

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

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

# bootstrap
import myapp
c = init(myapp)
svc = c.get(Service)
print(svc.run())

Output:

fetching from sqlite:///demo.db

Quick overrides for testing

from pico_ioc import init
import myapp

fake = {"repo": "fake-data"}
c = init(myapp, overrides={
    "fast_model": fake,                  # constant instance
    "user_service": lambda: {"id": 1},   # provider
})
assert c.get("fast_model") == {"repo": "fake-data"}

Scoped subgraphs

For unit tests or lightweight integration, you can bootstrap only a subset of the graph.

from pico_ioc
from src.runner_service import RunnerService
from tests.fakes import FakeDocker
import src

c = pico_ioc.scope(
    modules=[src],
    roots=[RunnerService],  # only RunnerService and its deps
    overrides={
        "docker.DockerClient": FakeDocker(),
    },
    strict=True,   # fail if something is missing
    lazy=True,     # instantiate on demand
)
svc = c.get(RunnerService)

This way you don’t need to bootstrap your entire app (controllers, http, …) just to test one service.


📖 Documentation


🧪 Development

pip install tox
tox

📜 Changelog

See CHANGELOG.md for version history.


📜 License

MIT — see 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-1.2.0.tar.gz (47.0 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-1.2.0-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pico_ioc-1.2.0.tar.gz
Algorithm Hash digest
SHA256 a7a07068894532aed302fa41a641cf2a53a48b73cab05352a3e69b01e236fa9d
MD5 344a414cd7d06f4608315e9251a5f329
BLAKE2b-256 2c6d687f83ab69bb249b0428357eaa89c0fc56171b7c4fe7a852746b093c5593

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pico_ioc-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 18.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-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d7d2b60bc2f306805afee205f2b9077af0b73c71c944c46a46614b7b4d892acf
MD5 c0f66467887d42d190eb2b4054f71793
BLAKE2b-256 e49d8e7178fdc9646ea22f86eb1b6e50d31d6967a71e769087ec10899b2bd227

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