{"id":33307614,"url":"https://github.com/dperezcabrera/pico-ioc","last_synced_at":"2026-02-06T22:03:06.155Z","repository":{"id":308917021,"uuid":"1034571342","full_name":"dperezcabrera/pico-ioc","owner":"dperezcabrera","description":"A minimalist, zero-dependency Inversion of Control (IoC) container for Python. 🚀 It uses decorators for automatic component discovery and lazy instantiation, helping you build clean, modular, and easily testable applications.","archived":false,"fork":false,"pushed_at":"2026-02-04T00:14:13.000Z","size":887,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-02-04T12:30:51.559Z","etag":null,"topics":["dependency-injection","di-container","inversion-of-control","inversion-of-control-container","ioc","ioc-container","ioc-containers","ioc-framework","minimalistic","python","zero-dependency"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dperezcabrera.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-08-08T15:53:36.000Z","updated_at":"2026-02-04T00:14:17.000Z","dependencies_parsed_at":"2025-08-08T17:48:09.102Z","dependency_job_id":"111bb171-3e77-4014-be5b-cb175930490c","html_url":"https://github.com/dperezcabrera/pico-ioc","commit_stats":null,"previous_names":["dperezcabrera/pico-ioc"],"tags_count":30,"template":false,"template_full_name":null,"purl":"pkg:github/dperezcabrera/pico-ioc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dperezcabrera%2Fpico-ioc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dperezcabrera%2Fpico-ioc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dperezcabrera%2Fpico-ioc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dperezcabrera%2Fpico-ioc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dperezcabrera","download_url":"https://codeload.github.com/dperezcabrera/pico-ioc/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dperezcabrera%2Fpico-ioc/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29178566,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-06T20:14:21.878Z","status":"ssl_error","status_checked_at":"2026-02-06T20:14:21.443Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dependency-injection","di-container","inversion-of-control","inversion-of-control-container","ioc","ioc-container","ioc-containers","ioc-framework","minimalistic","python","zero-dependency"],"created_at":"2025-11-19T00:00:27.012Z","updated_at":"2026-02-06T22:03:06.150Z","avatar_url":"https://github.com/dperezcabrera.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 📦 Pico-IoC: A Robust, Async-Native IoC Container for Python\n\n[![PyPI](https://img.shields.io/pypi/v/pico-ioc.svg)](https://pypi.org/project/pico-ioc/)\n[![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/dperezcabrera/pico-ioc)\n[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)\n![CI (tox matrix)](https://github.com/dperezcabrera/pico-ioc/actions/workflows/ci.yml/badge.svg)\n[![codecov](https://codecov.io/gh/dperezcabrera/pico-ioc/branch/main/graph/badge.svg)](https://codecov.io/gh/dperezcabrera/pico-ioc)\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=dperezcabrera_pico-ioc\u0026metric=alert_status)](https://sonarcloud.io/summary/new_code?id=dperezcabrera_pico-ioc)\n[![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=dperezcabrera_pico-ioc\u0026metric=duplicated_lines_density)](https://sonarcloud.io/summary/new_code?id=dperezcabrera_pico-ioc)\n[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=dperezcabrera_pico-ioc\u0026metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=dperezcabrera_pico-ioc)\n[![PyPI Downloads](https://static.pepy.tech/personalized-badge/pico-ioc?period=monthly\u0026units=INTERNATIONAL_SYSTEM\u0026left_color=BLACK\u0026right_color=GREEN\u0026left_text=Monthly+downloads)](https://pepy.tech/projects/pico-ioc)\n[![Docs](https://img.shields.io/badge/Docs-pico--ioc-blue?style=flat\u0026logo=readthedocs\u0026logoColor=white)](https://dperezcabrera.github.io/pico-ioc/)\n[![Interactive Lab](https://img.shields.io/badge/Learn-online-green?style=flat\u0026logo=python\u0026logoColor=white)](https://dperezcabrera.github.io/learn-pico-ioc/)\n\n**Pico-IoC** is a **lightweight, async-ready, decorator-driven IoC container** built for clarity, testability, and performance.\nIt brings Inversion of Control and dependency injection to Python in a deterministic, modern, and framework-agnostic way.\n\n\u003e 🐍 Requires Python 3.11+\n\n---\n\n## ⚖️ Core Principles\n\n- Single Purpose – Do one thing: dependency management.\n- Declarative – Use simple decorators (`@component`, `@factory`, `@provides`, `@configured`) instead of complex config files.\n- Deterministic – No hidden scanning or side-effects; everything flows from an explicit `init()`.\n- Async-Native – Fully supports async providers, async lifecycle hooks (`__ainit__`), and async interceptors.\n- Fail-Fast – Detects missing bindings and circular dependencies at bootstrap (`init()`).\n- Testable by Design – Use `overrides` and `profiles` to swap components instantly.\n- Zero Core Dependencies – Built entirely on the Python standard library. Optional features may require external packages (see Installation).\n\n---\n\n## 🚀 Why Pico-IoC?\n\nAs Python systems evolve, wiring dependencies by hand becomes fragile and unmaintainable.\nPico-IoC eliminates that friction by letting you declare how components relate — not how they’re created.\n\n| Feature         | Manual Wiring                  | With Pico-IoC                   |\n| :-------------- | :----------------------------- | :------------------------------ |\n| Object creation | `svc = Service(Repo(Config()))` | `svc = container.get(Service)`  |\n| Replacing deps  | Monkey-patch                   | `overrides={Repo: FakeRepo()}`  |\n| Coupling        | Tight                          | Loose                           |\n| Testing         | Painful                        | Instant                         |\n| Async support   | Manual                         | Built-in (`aget`, `__ainit__`)  |\n\n---\n\n## 🧩 Highlights (v2.2+)\n\n- **Unified Configuration**: Use `@configured` to bind both flat (ENV-like) and tree (YAML/JSON) sources via the `configuration(...)` builder (ADR-0010).\n- **Extensible Scanning**: Use `CustomScanner` to hook into the discovery phase and register functions or custom decorators (ADR-0011).\n- **Async-aware AOP**: Method interceptors via `@intercepted_by`.\n- **Scoped resolution**: singleton, prototype, request, session, transaction, and custom scopes.\n- **Tree-based configuration**: Advanced mapping with reusable adapters (`Annotated[Union[...], Discriminator(...)]`).\n- **Observable context**: Built-in stats, health checks (`@health`), observer hooks (`ContainerObserver`), and dependency graph export.\n\n---\n\n## 📦 Installation\n\n```bash\npip install pico-ioc\n```\n\nOptional extras:\n\n  - YAML configuration support (requires PyYAML)\n\n    ```bash\n    pip install pico-ioc[yaml]\n    ```\n\n-----\n\n### ⚠️ Important Note\n\n**Breaking Behavior in Scope Management (v2.1.3+):**\n**Scope LRU Eviction has been removed** to guarantee data integrity.\n\n  * **Frameworks (pico-fastapi):** Handled automatically.\n  * **Manual usage:** You **must** explicitly call `container._caches.cleanup_scope(\"scope_name\", scope_id)` when a context ends to prevent memory leaks.\n\n-----\n\n## ⚙️ Quick Example (Unified Configuration)\n\n```python\nimport os\nfrom dataclasses import dataclass\nfrom pico_ioc import component, configured, configuration, init, EnvSource\n\n# 1. Define configuration with @configured\n@configured(prefix=\"APP_\", mapping=\"auto\")  # Auto-detects flat mapping\n@dataclass\nclass Config:\n    db_url: str = \"sqlite:///demo.db\"\n\n# 2. Define components\n@component\nclass Repo:\n    def __init__(self, cfg: Config):  # Inject config\n        self.cfg = cfg\n    def fetch(self):\n        return f\"fetching from {self.cfg.db_url}\"\n\n@component\nclass Service:\n    def __init__(self, repo: Repo):  # Inject Repo\n        self.repo = repo\n    def run(self):\n        return self.repo.fetch()\n\n# --- Example Setup ---\nos.environ['APP_DB_URL'] = 'postgresql://user:pass@host/db'\n\n# 3. Build configuration context\nconfig_ctx = configuration(\n    EnvSource(prefix=\"\")  # Read APP_DB_URL from environment\n)\n\n# 4. Initialize container\ncontainer = init(modules=[__name__], config=config_ctx)  # Pass context via 'config'\n\n# 5. Get and use the service\nsvc = container.get(Service)\nprint(svc.run())\n\n# --- Cleanup ---\ndel os.environ['APP_DB_URL']\n```\n\nOutput:\n\n```\nfetching from postgresql://user:pass@host/db\n```\n\n-----\n\n## 🧪 Testing with Overrides\n\n```python\nclass FakeRepo:\n    def fetch(self): return \"fake-data\"\n\n# Build configuration context (might be empty or specific for test)\ntest_config_ctx = configuration()\n\n# Use overrides during init\ncontainer = init(\n    modules=[__name__],\n    config=test_config_ctx,\n    overrides={Repo: FakeRepo()}  # Replace Repo with FakeRepo\n)\n\nsvc = container.get(Service)\nassert svc.run() == \"fake-data\"\n```\n\n-----\n\n## 🧰 Profiles\n\nUse profiles to enable/disable components or configuration branches conditionally.\n\n```python\n# Enable \"test\" profile when bootstrapping the container\ncontainer = init(\n    modules=[__name__],\n    profiles=[\"test\"]\n)\n```\n\nProfiles are typically referenced in decorators or configuration mappings to include/exclude components and bindings.\n\n-----\n\n## ⚡ Async Components\n\nPico-IoC supports async lifecycle and resolution.\n\n```python\nimport asyncio\nfrom pico_ioc import component, init\n\n@component\nclass AsyncRepo:\n    async def __ainit__(self):\n        # e.g., open async connections\n        self.ready = True\n\n    async def fetch(self):\n        return \"async-data\"\n\nasync def main():\n    container = init(modules=[__name__])\n    repo = await container.aget(AsyncRepo)   # Async resolution\n    print(await repo.fetch())\n    \n    # Graceful async shutdown (calls @cleanup async methods)\n    await container.ashutdown()\n\nasyncio.run(main())\n```\n\n  - `__ainit__` runs after construction if defined.\n  - Use `container.aget(Type)` to resolve components that require async initialization.\n  - Use `await container.ashutdown()` to close resources cleanly.\n\n-----\n\n## 🩺 Lifecycle \u0026 AOP\n\n```python\nimport time\nfrom pico_ioc import component, init, intercepted_by, MethodInterceptor, MethodCtx\n\n# Define an interceptor component\n@component\nclass LogInterceptor(MethodInterceptor):\n    def invoke(self, ctx: MethodCtx, call_next):\n        print(f\"→ calling {ctx.cls.__name__}.{ctx.name}\")\n        start = time.perf_counter()\n        try:\n            res = call_next(ctx)\n            duration = (time.perf_counter() - start) * 1000\n            print(f\"← {ctx.cls.__name__}.{ctx.name} done ({duration:.2f}ms)\")\n            return res\n        except Exception as e:\n            duration = (time.perf_counter() - start) * 1000\n            print(f\"← {ctx.cls.__name__}.{ctx.name} failed ({duration:.2f}ms): {e}\")\n            raise\n\n@component\nclass Demo:\n    @intercepted_by(LogInterceptor)  # Apply the interceptor\n    def work(self):\n        print(\"   Working...\")\n        time.sleep(0.01)\n        return \"ok\"\n\n# Initialize container (must scan module containing interceptor too)\nc = init(modules=[__name__])\nresult = c.get(Demo).work()\nprint(f\"Result: {result}\")\n```\n\n-----\n\n## 👁️ Observability \u0026 Cleanup\n\n  - Export a dependency graph in DOT format:\n\n    ```python\n    c = init(modules=[...])\n    c.export_graph(\"dependencies.dot\")  # Writes directly to file\n    ```\n\n  - Health checks:\n\n      - Annotate health probes inside components with `@health` for container-level reporting.\n      - The container exposes health information that can be queried in observability tooling.\n\n  - Container cleanup:\n\n      - For sync apps: `container.shutdown()`\n      - For async apps: `await container.ashutdown()`\n\nUse cleanup in application shutdown hooks to release resources deterministically.\n\n-----\n\n## 📖 Documentation\n\nThe full documentation is available within the `docs/` directory of the project repository. Start with `docs/README.md` for navigation.\n\n  - Getting Started: `docs/getting-started.md`\n  - User Guide: `docs/user-guide/README.md`\n  - Advanced Features: `docs/advanced-features/README.md`\n  - Observability: `docs/observability/README.md`\n  - Cookbook (Patterns): `docs/cookbook/README.md`\n  - Architecture: `docs/architecture/README.md`\n  - API Reference: `docs/api-reference/README.md`\n  - ADR Index: `docs/adr/README.md`\n\n-----\n\n## 🧩 Development\n\n```bash\npip install tox\ntox\n```\n\n-----\n\n## 🧾 Changelog\n\nSee [CHANGELOG.md](./CHANGELOG.md) — Significant redesigns and features in v2.0+.\n\n-----\n\n## 🤖 Claude Code Skills\n\nThis project includes pre-designed skills for [Claude Code](https://claude.ai/claude-code), enabling AI-assisted development with pico-ioc patterns.\n\n| Skill | Command | Description |\n|-------|---------|-------------|\n| **Pico Component Creator** | `/pico-component` | Creates components with DI, scopes, factories and interceptors |\n| **Pico Test Generator** | `/pico-tests` | Generates tests for pico-framework components |\n\nSee [Skills documentation](docs/skills.md) for full details and installation instructions.\n\n-----\n\n## 📜 License\n\nMIT — [LICENSE](https://opensource.org/licenses/MIT)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdperezcabrera%2Fpico-ioc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdperezcabrera%2Fpico-ioc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdperezcabrera%2Fpico-ioc/lists"}