How yakimka/picodi works
Project Type and Purpose
• Picodi is a Python library (not a standalone app) implementing Dependency Injection (DI).
• It provides decorators and a registry to manage lifecycles and scopes (singleton, request, etc.) in both sync and async code.
• It integrates with frameworks like FastAPI, Starlette, and pytest, and ships with docs, tests, CI pipelines, and benchmark data.
a. Source Code (“picodi/” folder)
• Core modules:
– _state.py: holds the global registry and storage for scoped instances.
– _scopes.py: defines scope classes (SingletonScope, NullScope, custom scopes).
– _inject_decorator.py & _internal.py: implement @inject decorator and plumbing.
– registry.py: API for registering providers, setting scopes, and lifecycle management (alifespan).
– helpers.py & support.py: utility functions (get_value, resolving logic).
• Types and interfaces: _types.py.
• Integrations:
– fastapi.py, starlette.py, _pytest.py, _pytest_asyncio.py under picodi/integrations.
• FastAPI / Starlette application
• pytest test suites (sync and async plugins)
c. Configuration & Tooling
• CI/CD: .github/workflows/workflow-ci.yml, ci.sh, Makefile (test, lint, build)
• Docker: Dockerfile-dev, docker-compose.yml
• Documentation: docs/ (Sphinx sources, tutorials, API reference)
• Benchmark data: .benchmarks/ (JSON results for different Python versions)
d. Tests (“tests/” folder)
• Core module tests
• Integration tests for frameworks
• Registry & lifecycle tests
• Scope behavior tests
• Benchmarks and race-condition tests
Relationships and Data Flows
• User Code invokes functions/classes decorated with @inject →
InjectDecorator (picodi/_inject_decorator.py) intercepts call →
Delegates to Registry (picodi/registry.py) to resolve dependencies →
Registry consults Scope Manager (picodi/_scopes.py) →
• For each dependency provider, checks/_state stores or initializes instance →
• Yields instances up the chain →
Decorator invokes original function with fully resolved args →
If async, coroutines are awaited; if sync, direct call.
• Lifecycle (alifespan) hooks into application startup/shutdown →
Pre-initializes providers marked auto_init →
Cleans up resources at shutdown.
• Integrations modules register framework-specific startup/shutdown hooks and injection points (FastAPI dependencies, pytest fixtures).
Architectural Patterns and Principles
• Inversion of Control via decorators and registry
• Scope management for lifecycles (Singleton, ContextVar-based, null, custom)
• Pluggable integration points (strategy pattern) for frameworks
• Zero external deps; pure-Python modular design
• Separation of concerns: core logic vs. integrations vs. docs vs. tests vs. CI