pico-ioc 1.4.0__py3-none-any.whl → 2.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pico_ioc/__init__.py +91 -45
- pico_ioc/_version.py +1 -1
- pico_ioc/aop.py +247 -0
- pico_ioc/api.py +791 -207
- pico_ioc/config_runtime.py +289 -0
- pico_ioc/constants.py +10 -0
- pico_ioc/container.py +289 -189
- pico_ioc/event_bus.py +224 -0
- pico_ioc/exceptions.py +66 -0
- pico_ioc/factory.py +48 -0
- pico_ioc/locator.py +53 -0
- pico_ioc/scope.py +106 -40
- pico_ioc-2.0.0.dist-info/METADATA +230 -0
- pico_ioc-2.0.0.dist-info/RECORD +17 -0
- pico_ioc/_state.py +0 -75
- pico_ioc/builder.py +0 -294
- pico_ioc/config.py +0 -332
- pico_ioc/decorators.py +0 -158
- pico_ioc/interceptors.py +0 -56
- pico_ioc/plugins.py +0 -28
- pico_ioc/policy.py +0 -245
- pico_ioc/proxy.py +0 -129
- pico_ioc/public_api.py +0 -76
- pico_ioc/resolver.py +0 -132
- pico_ioc/scanner.py +0 -203
- pico_ioc/utils.py +0 -25
- pico_ioc-1.4.0.dist-info/METADATA +0 -241
- pico_ioc-1.4.0.dist-info/RECORD +0 -22
- {pico_ioc-1.4.0.dist-info → pico_ioc-2.0.0.dist-info}/WHEEL +0 -0
- {pico_ioc-1.4.0.dist-info → pico_ioc-2.0.0.dist-info}/licenses/LICENSE +0 -0
- {pico_ioc-1.4.0.dist-info → pico_ioc-2.0.0.dist-info}/top_level.txt +0 -0
pico_ioc/scanner.py
DELETED
|
@@ -1,203 +0,0 @@
|
|
|
1
|
-
# src/pico_ioc/scanner.py
|
|
2
|
-
from __future__ import annotations
|
|
3
|
-
|
|
4
|
-
import importlib
|
|
5
|
-
import inspect
|
|
6
|
-
import logging
|
|
7
|
-
import pkgutil
|
|
8
|
-
from types import ModuleType
|
|
9
|
-
from typing import Any, Callable, Optional, Tuple, List, Iterable
|
|
10
|
-
|
|
11
|
-
from .plugins import run_plugin_hook, PicoPlugin
|
|
12
|
-
from .container import PicoContainer, Binder
|
|
13
|
-
from .decorators import (
|
|
14
|
-
COMPONENT_FLAG,
|
|
15
|
-
COMPONENT_KEY,
|
|
16
|
-
COMPONENT_LAZY,
|
|
17
|
-
FACTORY_FLAG,
|
|
18
|
-
PROVIDES_KEY,
|
|
19
|
-
PROVIDES_LAZY,
|
|
20
|
-
COMPONENT_TAGS,
|
|
21
|
-
PROVIDES_TAGS,
|
|
22
|
-
INTERCEPTOR_META,
|
|
23
|
-
)
|
|
24
|
-
from .proxy import ComponentProxy
|
|
25
|
-
from .resolver import Resolver
|
|
26
|
-
from . import _state
|
|
27
|
-
from .utils import _provider_from_class, _provider_from_callable
|
|
28
|
-
from .config import is_config_component, build_component_instance, ConfigRegistry
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
def scan_and_configure(
|
|
32
|
-
package_or_name: Any,
|
|
33
|
-
container: PicoContainer,
|
|
34
|
-
*,
|
|
35
|
-
exclude: Optional[Callable[[str], bool]] = None,
|
|
36
|
-
plugins: Tuple[PicoPlugin, ...] = (),
|
|
37
|
-
) -> tuple[int, int, list[tuple[Any, dict]]]:
|
|
38
|
-
"""
|
|
39
|
-
Scan a package, bind components/factories, and collect interceptor declarations.
|
|
40
|
-
|
|
41
|
-
Returns: (component_count, factory_count, interceptor_decls)
|
|
42
|
-
- interceptor_decls entries:
|
|
43
|
-
(cls, meta) for @interceptor class
|
|
44
|
-
(fn, meta) for @interceptor function
|
|
45
|
-
((owner_cls, fn), meta) for @interceptor methods
|
|
46
|
-
"""
|
|
47
|
-
package = _as_module(package_or_name)
|
|
48
|
-
logging.info("Scanning in '%s'...", getattr(package, "__name__", repr(package)))
|
|
49
|
-
|
|
50
|
-
binder = Binder(container)
|
|
51
|
-
resolver = Resolver(container)
|
|
52
|
-
|
|
53
|
-
run_plugin_hook(plugins, "before_scan", package, binder)
|
|
54
|
-
|
|
55
|
-
comp_classes, factory_classes, interceptor_decls = _collect_decorated(
|
|
56
|
-
package=package,
|
|
57
|
-
exclude=exclude,
|
|
58
|
-
plugins=plugins,
|
|
59
|
-
binder=binder,
|
|
60
|
-
)
|
|
61
|
-
|
|
62
|
-
run_plugin_hook(plugins, "after_scan", package, binder)
|
|
63
|
-
|
|
64
|
-
_register_component_classes(classes=comp_classes, container=container, resolver=resolver)
|
|
65
|
-
_register_factory_classes(factory_classes=factory_classes, container=container, resolver=resolver)
|
|
66
|
-
|
|
67
|
-
return len(comp_classes), len(factory_classes), interceptor_decls
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
# -------------------- helpers --------------------
|
|
71
|
-
|
|
72
|
-
def _as_module(package_or_name: Any) -> ModuleType:
|
|
73
|
-
if isinstance(package_or_name, str):
|
|
74
|
-
return importlib.import_module(package_or_name)
|
|
75
|
-
if hasattr(package_or_name, "__spec__"):
|
|
76
|
-
return package_or_name # type: ignore[return-value]
|
|
77
|
-
raise TypeError("package_or_name must be a module or importable package name (str).")
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
def _iter_package_modules(package: ModuleType) -> Iterable[str]:
|
|
81
|
-
"""Yield fully-qualified module names under a package (recursive)."""
|
|
82
|
-
try:
|
|
83
|
-
pkg_path = package.__path__ # type: ignore[attr-defined]
|
|
84
|
-
except Exception:
|
|
85
|
-
return
|
|
86
|
-
prefix = package.__name__ + "."
|
|
87
|
-
for _finder, name, _is_pkg in pkgutil.walk_packages(pkg_path, prefix):
|
|
88
|
-
yield name
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
def _collect_decorated(
|
|
92
|
-
*,
|
|
93
|
-
package: ModuleType,
|
|
94
|
-
exclude: Optional[Callable[[str], bool]],
|
|
95
|
-
plugins: Tuple[PicoPlugin, ...],
|
|
96
|
-
binder: Binder,
|
|
97
|
-
) -> Tuple[List[type], List[type], List[tuple[Any, dict]]]:
|
|
98
|
-
comps: List[type] = []
|
|
99
|
-
facts: List[type] = []
|
|
100
|
-
interceptors: List[tuple[Any, dict]] = []
|
|
101
|
-
|
|
102
|
-
def _collect_from_class(cls: type):
|
|
103
|
-
if getattr(cls, COMPONENT_FLAG, False):
|
|
104
|
-
comps.append(cls)
|
|
105
|
-
elif getattr(cls, FACTORY_FLAG, False):
|
|
106
|
-
facts.append(cls)
|
|
107
|
-
|
|
108
|
-
meta_class = getattr(cls, INTERCEPTOR_META, None)
|
|
109
|
-
if meta_class:
|
|
110
|
-
interceptors.append((cls, dict(meta_class)))
|
|
111
|
-
|
|
112
|
-
for _nm, fn in inspect.getmembers(cls, predicate=inspect.isfunction):
|
|
113
|
-
meta_m = getattr(fn, INTERCEPTOR_META, None)
|
|
114
|
-
if meta_m:
|
|
115
|
-
interceptors.append(((cls, fn), dict(meta_m)))
|
|
116
|
-
|
|
117
|
-
def _visit_module(module: ModuleType):
|
|
118
|
-
for _name, obj in inspect.getmembers(module, inspect.isclass):
|
|
119
|
-
run_plugin_hook(plugins, "visit_class", module, obj, binder)
|
|
120
|
-
_collect_from_class(obj)
|
|
121
|
-
|
|
122
|
-
for _name, fn in inspect.getmembers(module, predicate=inspect.isfunction):
|
|
123
|
-
meta = getattr(fn, INTERCEPTOR_META, None)
|
|
124
|
-
if meta:
|
|
125
|
-
interceptors.append((fn, dict(meta)))
|
|
126
|
-
|
|
127
|
-
for mod_name in _iter_package_modules(package):
|
|
128
|
-
if exclude and exclude(mod_name):
|
|
129
|
-
logging.info("Skipping module %s (excluded)", mod_name)
|
|
130
|
-
continue
|
|
131
|
-
try:
|
|
132
|
-
module = importlib.import_module(mod_name)
|
|
133
|
-
except Exception as e:
|
|
134
|
-
logging.warning("Module %s not processed: %s", mod_name, e)
|
|
135
|
-
continue
|
|
136
|
-
_visit_module(module)
|
|
137
|
-
|
|
138
|
-
if not hasattr(package, "__path__"):
|
|
139
|
-
_visit_module(package)
|
|
140
|
-
|
|
141
|
-
return comps, facts, interceptors
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
def _register_component_classes(
|
|
145
|
-
*,
|
|
146
|
-
classes: List[type],
|
|
147
|
-
container: PicoContainer,
|
|
148
|
-
resolver: Resolver,
|
|
149
|
-
) -> None:
|
|
150
|
-
for cls in classes:
|
|
151
|
-
key = getattr(cls, COMPONENT_KEY, cls)
|
|
152
|
-
is_lazy = bool(getattr(cls, COMPONENT_LAZY, False))
|
|
153
|
-
tags = tuple(getattr(cls, COMPONENT_TAGS, ()))
|
|
154
|
-
if is_config_component(cls):
|
|
155
|
-
registry: ConfigRegistry | None = getattr(container, "_config_registry", None)
|
|
156
|
-
def _prov(_c=cls, _reg=registry):
|
|
157
|
-
if _reg is None:
|
|
158
|
-
raise RuntimeError(f"No config registry found to build {_c.__name__}")
|
|
159
|
-
return build_component_instance(_c, _reg)
|
|
160
|
-
provider = (lambda p=_prov: ComponentProxy(p)) if is_lazy else _prov
|
|
161
|
-
else:
|
|
162
|
-
provider = _provider_from_class(cls, resolver=resolver, lazy=is_lazy)
|
|
163
|
-
container.bind(key, provider, lazy=is_lazy, tags=tags)
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
def _register_factory_classes(
|
|
167
|
-
*,
|
|
168
|
-
factory_classes: List[type],
|
|
169
|
-
container: PicoContainer,
|
|
170
|
-
resolver: Resolver,
|
|
171
|
-
) -> None:
|
|
172
|
-
for fcls in factory_classes:
|
|
173
|
-
try:
|
|
174
|
-
tok_res = _state._resolving.set(True)
|
|
175
|
-
try:
|
|
176
|
-
finst = resolver.create_instance(fcls)
|
|
177
|
-
finally:
|
|
178
|
-
_state._resolving.reset(tok_res)
|
|
179
|
-
except Exception:
|
|
180
|
-
logging.exception("Error in factory %s", fcls.__name__)
|
|
181
|
-
continue
|
|
182
|
-
|
|
183
|
-
for attr_name, func in inspect.getmembers(fcls, predicate=inspect.isfunction):
|
|
184
|
-
provided_key = getattr(func, PROVIDES_KEY, None)
|
|
185
|
-
if provided_key is None:
|
|
186
|
-
continue
|
|
187
|
-
|
|
188
|
-
is_lazy = bool(getattr(func, PROVIDES_LAZY, False))
|
|
189
|
-
tags = tuple(getattr(func, PROVIDES_TAGS, ()))
|
|
190
|
-
|
|
191
|
-
bound = getattr(finst, attr_name, func.__get__(finst, fcls))
|
|
192
|
-
prov = _provider_from_callable(bound, owner_cls=fcls, resolver=resolver, lazy=is_lazy)
|
|
193
|
-
|
|
194
|
-
if isinstance(provided_key, type):
|
|
195
|
-
try:
|
|
196
|
-
setattr(prov, "_pico_alias_for", provided_key)
|
|
197
|
-
except Exception:
|
|
198
|
-
pass
|
|
199
|
-
unique_key = (provided_key, f"{fcls.__name__}.{attr_name}")
|
|
200
|
-
container.bind(unique_key, prov, lazy=is_lazy, tags=tags)
|
|
201
|
-
else:
|
|
202
|
-
container.bind(provided_key, prov, lazy=is_lazy, tags=tags)
|
|
203
|
-
|
pico_ioc/utils.py
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
# src/pico_ioc/utils.py
|
|
2
|
-
from typing import Any, Callable
|
|
3
|
-
from .container import PicoContainer
|
|
4
|
-
from .proxy import ComponentProxy
|
|
5
|
-
|
|
6
|
-
def _wrap_if_lazy(provider: Callable, is_lazy: bool) -> Callable:
|
|
7
|
-
"""Wraps a provider in a ComponentProxy if it's marked as lazy."""
|
|
8
|
-
return (lambda: ComponentProxy(provider)) if is_lazy else provider
|
|
9
|
-
|
|
10
|
-
def _provider_from_class(cls: type, *, resolver, lazy: bool):
|
|
11
|
-
def _new():
|
|
12
|
-
return resolver.create_instance(cls)
|
|
13
|
-
return _wrap_if_lazy(_new, lazy)
|
|
14
|
-
|
|
15
|
-
def _provider_from_callable(fn, *, owner_cls, resolver, lazy: bool):
|
|
16
|
-
def _invoke():
|
|
17
|
-
kwargs = resolver.kwargs_for_callable(fn, owner_cls=owner_cls)
|
|
18
|
-
return fn(**kwargs)
|
|
19
|
-
return _wrap_if_lazy(_invoke, lazy)
|
|
20
|
-
|
|
21
|
-
def create_alias_provider(container: PicoContainer, target_key: Any) -> Callable[[], Any]:
|
|
22
|
-
"""Creates a provider that delegates the get() call to the container for another key."""
|
|
23
|
-
def _provider():
|
|
24
|
-
return container.get(target_key)
|
|
25
|
-
return _provider
|
|
@@ -1,241 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: pico-ioc
|
|
3
|
-
Version: 1.4.0
|
|
4
|
-
Summary: A minimalist, zero-dependency Inversion of Control (IoC) container for Python.
|
|
5
|
-
Author-email: David Perez Cabrera <dperezcabrera@gmail.com>
|
|
6
|
-
License: MIT License
|
|
7
|
-
|
|
8
|
-
Copyright (c) 2025 David Pérez Cabrera
|
|
9
|
-
|
|
10
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
-
in the Software without restriction, including without limitation the rights
|
|
13
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
-
furnished to do so, subject to the following conditions:
|
|
16
|
-
|
|
17
|
-
The above copyright notice and this permission notice shall be included in all
|
|
18
|
-
copies or substantial portions of the Software.
|
|
19
|
-
|
|
20
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
-
SOFTWARE.
|
|
27
|
-
|
|
28
|
-
Project-URL: Homepage, https://github.com/dperezcabrera/pico-ioc
|
|
29
|
-
Project-URL: Repository, https://github.com/dperezcabrera/pico-ioc
|
|
30
|
-
Project-URL: Issue Tracker, https://github.com/dperezcabrera/pico-ioc/issues
|
|
31
|
-
Keywords: ioc,di,dependency injection,inversion of control,decorator
|
|
32
|
-
Classifier: Development Status :: 4 - Beta
|
|
33
|
-
Classifier: Programming Language :: Python :: 3
|
|
34
|
-
Classifier: Programming Language :: Python :: 3 :: Only
|
|
35
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
36
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
37
|
-
Classifier: Programming Language :: Python :: 3.12
|
|
38
|
-
Classifier: Programming Language :: Python :: 3.13
|
|
39
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
40
|
-
Classifier: Operating System :: OS Independent
|
|
41
|
-
Requires-Python: >=3.8
|
|
42
|
-
Description-Content-Type: text/markdown
|
|
43
|
-
License-File: LICENSE
|
|
44
|
-
Dynamic: license-file
|
|
45
|
-
|
|
46
|
-
# 📦 Pico-IoC: A Minimalist IoC Container for Python
|
|
47
|
-
|
|
48
|
-
[](https://pypi.org/project/pico-ioc/)
|
|
49
|
-
[](https://deepwiki.com/dperezcabrera/pico-ioc)
|
|
50
|
-
[](https://opensource.org/licenses/MIT)
|
|
51
|
-

|
|
52
|
-
[](https://codecov.io/gh/dperezcabrera/pico-ioc)
|
|
53
|
-
[](https://sonarcloud.io/summary/new_code?id=dperezcabrera_pico-ioc)
|
|
54
|
-
[](https://sonarcloud.io/summary/new_code?id=dperezcabrera_pico-ioc)
|
|
55
|
-
[](https://sonarcloud.io/summary/new_code?id=dperezcabrera_pico-ioc)
|
|
56
|
-
|
|
57
|
-
**pico-ioc** is a **tiny, zero-dependency, decorator-based IoC container for Python**.
|
|
58
|
-
It helps you build loosely-coupled, testable apps without manual wiring. Inspired by the Spring ecosystem, but minimal.
|
|
59
|
-
|
|
60
|
-
> ⚠️ **Requires Python 3.10+** (uses `typing.Annotated` and `include_extras=True`).
|
|
61
|
-
|
|
62
|
-
---
|
|
63
|
-
|
|
64
|
-
## ⚖️ Principles
|
|
65
|
-
|
|
66
|
-
* **Focus & Simplicity**: A minimal core for one job: managing dependencies. It avoids accidental complexity by doing one thing well.
|
|
67
|
-
* **Predictable & Explicit**: No magic. Behavior is deterministic, relying on explicit decorators and a clear resolution order.
|
|
68
|
-
* **Unified Composition Root**: The application is assembled from a single entry point (`init`) which defines a clear, predictable boundary. This ensures a stable and understandable bootstrap process.
|
|
69
|
-
* **Fail-Fast Bootstrap**: Catches dependency graph errors at startup, not in production. If the application runs, it's wired correctly.
|
|
70
|
-
* **Testability First**: Features like `scope()` and `overrides` are first-class citizens, enabling fast and isolated testing.
|
|
71
|
-
* **Extensible by Design**: Lifecycle hooks and AOP are available through a clean Plugin and Interceptor API without altering the core.
|
|
72
|
-
* **Framework Agnostic**: Zero hard dependencies. It works with any Python application, from simple scripts to complex web servers.
|
|
73
|
-
|
|
74
|
-
---
|
|
75
|
-
|
|
76
|
-
## ✨ Why Pico-IoC?
|
|
77
|
-
|
|
78
|
-
`pico-ioc` exists to solve a common problem that arises as Python applications grow: managing how objects are created and connected becomes complex and brittle. This manual wiring, where a change deep in the application can cause a cascade of updates, makes the code hard to test and maintain. `pico-ioc` introduces the principle of Inversion of Control (IoC) in a simple, Pythonic way. Instead of you creating and connecting every object, you declare your components with a simple `@component` decorator, and the container automatically wires them together based on their type hints. It brings the architectural robustness and testability of mature frameworks like Spring to the Python ecosystem, but without the heavy boilerplate, allowing you to build complex, loosely-coupled applications that remain simple to manage.
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
| Feature | Manual Wiring | With Pico-IoC |
|
|
82
|
-
| :------------------ | :------------------------------------------------ | :------------------------------ |
|
|
83
|
-
| **Object Creation** | `service = Service(Repo(Config()))` | `svc = container.get(Service)` |
|
|
84
|
-
| **Testing** | Manual replacement or monkey-patching | `overrides={Repo: FakeRepo()}` |
|
|
85
|
-
| **Coupling** | High (code knows about constructors) | Low (code just asks for a type) |
|
|
86
|
-
| **Maintenance** | Brittle (changing a constructor breaks consumers) | Robust (changes are isolated) |
|
|
87
|
-
| **Learning Curve** | Ad-hoc, implicit patterns | Uniform, explicit, documented |
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
---
|
|
91
|
-
|
|
92
|
-
## 🧩 Features
|
|
93
|
-
|
|
94
|
-
### Core
|
|
95
|
-
|
|
96
|
-
* **Zero dependencies** — pure Python, framework-agnostic.
|
|
97
|
-
* **Single Entry Point (`init`)** — Robustly bootstrap your entire application from a single root package, enforcing a clean "Composition Root" pattern.
|
|
98
|
-
* **Decorator API** — `@component`, `@factory_component`, `@provides`, `@plugin`.
|
|
99
|
-
* **Fail-fast bootstrap** — eager by default; missing deps surface at startup.
|
|
100
|
-
* **Opt-in lazy** — `lazy=True` wraps with `ComponentProxy`.
|
|
101
|
-
* **Smart resolution order** — parameter name → type annotation → MRO → string.
|
|
102
|
-
* **Overrides for testing** — inject mocks/fakes directly via `init(overrides={...})`.
|
|
103
|
-
* **Public API helper** — auto-export decorated symbols in `__init__.py`.
|
|
104
|
-
* **Thread/async safe** — isolation via `ContextVar`.
|
|
105
|
-
|
|
106
|
-
### Advanced
|
|
107
|
-
|
|
108
|
-
* **Qualifiers & collections** — `list[Annotated[T, Q]]` filters by qualifier.
|
|
109
|
-
* **Flexible Scopes (`scope`)** — Create lightweight, temporary containers from multiple modules, ideal for testing, scripting, or modular tasks.
|
|
110
|
-
* **Interceptors API** — observe/modify resolution, instantiation, invocation, errors.
|
|
111
|
-
* **Conditional providers** — activate components by env vars or predicates.
|
|
112
|
-
* **Plugins** — lifecycle hooks (`before_scan`, `after_ready`).
|
|
113
|
-
|
|
114
|
-
---
|
|
115
|
-
|
|
116
|
-
## 📦 Installation
|
|
117
|
-
|
|
118
|
-
```bash
|
|
119
|
-
# Requires Python 3.10+
|
|
120
|
-
pip install pico-ioc
|
|
121
|
-
````
|
|
122
|
-
|
|
123
|
-
---
|
|
124
|
-
|
|
125
|
-
## 🚀 Quick start
|
|
126
|
-
|
|
127
|
-
```python
|
|
128
|
-
from pico_ioc import component, init
|
|
129
|
-
|
|
130
|
-
@component
|
|
131
|
-
class Config:
|
|
132
|
-
url = "sqlite:///demo.db"
|
|
133
|
-
|
|
134
|
-
@component
|
|
135
|
-
class Repo:
|
|
136
|
-
def __init__(self, cfg: Config):
|
|
137
|
-
self.url = cfg.url
|
|
138
|
-
def fetch(self): return f"fetching from {self.url}"
|
|
139
|
-
|
|
140
|
-
@component
|
|
141
|
-
class Service:
|
|
142
|
-
def __init__(self, repo: Repo):
|
|
143
|
-
self.repo = repo
|
|
144
|
-
def run(self): return self.repo.fetch()
|
|
145
|
-
|
|
146
|
-
# bootstrap
|
|
147
|
-
import myapp
|
|
148
|
-
c = init(myapp)
|
|
149
|
-
svc = c.get(Service)
|
|
150
|
-
print(svc.run())
|
|
151
|
-
```
|
|
152
|
-
|
|
153
|
-
**Output:**
|
|
154
|
-
|
|
155
|
-
```
|
|
156
|
-
fetching from sqlite:///demo.db
|
|
157
|
-
```
|
|
158
|
-
---
|
|
159
|
-
|
|
160
|
-
### Quick overrides for testing
|
|
161
|
-
|
|
162
|
-
```python
|
|
163
|
-
from pico_ioc import init
|
|
164
|
-
import myapp
|
|
165
|
-
|
|
166
|
-
fake = {"repo": "fake-data"}
|
|
167
|
-
c = init(myapp, overrides={
|
|
168
|
-
"fast_model": fake, # constant instance
|
|
169
|
-
"user_service": lambda: {"id": 1}, # provider
|
|
170
|
-
})
|
|
171
|
-
assert c.get("fast_model") == {"repo": "fake-data"}
|
|
172
|
-
```
|
|
173
|
-
---
|
|
174
|
-
|
|
175
|
-
### Scoped subgraphs
|
|
176
|
-
|
|
177
|
-
For unit tests or lightweight integration, you can bootstrap **only a subset of the graph**.
|
|
178
|
-
|
|
179
|
-
```python
|
|
180
|
-
from pico_ioc
|
|
181
|
-
from src.runner_service import RunnerService
|
|
182
|
-
from tests.fakes import FakeDocker
|
|
183
|
-
import src
|
|
184
|
-
|
|
185
|
-
c = pico_ioc.scope(
|
|
186
|
-
modules=[src],
|
|
187
|
-
roots=[RunnerService], # only RunnerService and its deps
|
|
188
|
-
overrides={
|
|
189
|
-
"docker.DockerClient": FakeDocker(),
|
|
190
|
-
},
|
|
191
|
-
strict=True, # fail if something is missing
|
|
192
|
-
lazy=True, # instantiate on demand
|
|
193
|
-
)
|
|
194
|
-
svc = c.get(RunnerService)
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
This way you don’t need to bootstrap your entire app (`controllers`, `http`, …) just to test one service.
|
|
198
|
-
|
|
199
|
-
---
|
|
200
|
-
## 📖 Documentation
|
|
201
|
-
|
|
202
|
-
* **🚀 New to pico-ioc? Start with the User Guide.**
|
|
203
|
-
* [**GUIDE.md**](.llm/GUIDE.md) — Learn with practical examples: testing, configuration, collection injection, and web framework integration.
|
|
204
|
-
|
|
205
|
-
* **🏗️ Want to understand the internals? See the Architecture.**
|
|
206
|
-
* [**ARCHITECTURE.md**](.llm/ARCHITECTURE.md) — A deep dive into the algorithms, lifecycle, and internal diagrams. Perfect for contributors.
|
|
207
|
-
|
|
208
|
-
* **🤔 Want to know *why* it's designed this way? Read the Decisions.**
|
|
209
|
-
* [**DECISIONS.md**](.llm/DECISIONS.md) — The history and rationale behind key technical decisions.
|
|
210
|
-
|
|
211
|
-
* **💡 Just need a quick summary?**
|
|
212
|
-
* [**OVERVIEW.md**](.llm/OVERVIEW.md) — What pico-ioc is and why you should use it.
|
|
213
|
-
---
|
|
214
|
-
|
|
215
|
-
## 🧪 Development
|
|
216
|
-
|
|
217
|
-
```bash
|
|
218
|
-
pip install tox
|
|
219
|
-
tox
|
|
220
|
-
```
|
|
221
|
-
|
|
222
|
-
---
|
|
223
|
-
|
|
224
|
-
## 📜 Overview
|
|
225
|
-
|
|
226
|
-
See [OVERVIEW.md](.llm/OVERVIEW.md) Just need a quick summary?
|
|
227
|
-
|
|
228
|
-
---
|
|
229
|
-
|
|
230
|
-
## 📜 Changelog
|
|
231
|
-
|
|
232
|
-
See [CHANGELOG.md](./CHANGELOG.md) for version history.
|
|
233
|
-
|
|
234
|
-
---
|
|
235
|
-
|
|
236
|
-
## 📜 License
|
|
237
|
-
|
|
238
|
-
MIT — see [LICENSE](https://opensource.org/licenses/MIT)
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
pico_ioc-1.4.0.dist-info/RECORD
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
pico_ioc/__init__.py,sha256=s_9v-pMM5X7r5vhbzaOmQQEHBLOmZCV7o6QNtRcfMAU,1282
|
|
2
|
-
pico_ioc/_state.py,sha256=C98XQZIfKy98j8fzR730eUCoqSnCkRkxUS4bH7mp73c,2154
|
|
3
|
-
pico_ioc/_version.py,sha256=EyMGX1ADFzN6XVXHWbJUtKPONYKeFkvWoKIFPDDB2I8,22
|
|
4
|
-
pico_ioc/api.py,sha256=cc9c3db6_dIfeobP_VvFRpNV9qNIsIPbUct5X9mmW9w,8348
|
|
5
|
-
pico_ioc/builder.py,sha256=ZvIpOaAzBpByw-u5V52GM5cJAMH_E_5FMDLlgyqd-g4,11379
|
|
6
|
-
pico_ioc/config.py,sha256=J3k7_2vRB2HCpikzeMzT4Ut9COFM4kcydkwZorncqSk,12317
|
|
7
|
-
pico_ioc/container.py,sha256=V9X0qvNPZYU80C65X3Dqifek6RWt9kgEwG0CkX1Hpow,6461
|
|
8
|
-
pico_ioc/decorators.py,sha256=Jyq7PhSM3uFVfBEaCq6x_mFV9V3B5fTEK4o3I6ZvG5A,4492
|
|
9
|
-
pico_ioc/interceptors.py,sha256=rBdpI7ca5L30N-zR7LKroCIc5FgfNb9M5P7OEGw-TtY,1955
|
|
10
|
-
pico_ioc/plugins.py,sha256=GP7WEMshggQ-FEjiShkcuLrSMxfueUnhbY9I8PcIyPU,1039
|
|
11
|
-
pico_ioc/policy.py,sha256=p7maTHNfU-zoaz3j7CY4P3ry-bYfaGxAOklcTAuF6dY,8648
|
|
12
|
-
pico_ioc/proxy.py,sha256=VJA-QaO8yvejcHmX5mlXMHfyuyXFxD7cazONSzBGrf0,6308
|
|
13
|
-
pico_ioc/public_api.py,sha256=E3sArCoI1xxkIw7xQBvLYAWcIoVJjcq1s0kH-0qIVDE,2383
|
|
14
|
-
pico_ioc/resolver.py,sha256=clIS9wwhOKzIwzBQFXxCrmPX2gM2X2eVyS8P_VEeyDw,4798
|
|
15
|
-
pico_ioc/scanner.py,sha256=TmDLkklO-e2LBoVducQD4-uuZKFDg_dMwgwO9vM8-pU,7129
|
|
16
|
-
pico_ioc/scope.py,sha256=5oRCir1Dqu8Jlgl_R-q900my1u6_7zq5VUbq8ahV280,1754
|
|
17
|
-
pico_ioc/utils.py,sha256=OyhOKnyepwGQ_uQKlQLt-fymEV1bQ6hCq4Me7h3dfco,1002
|
|
18
|
-
pico_ioc-1.4.0.dist-info/licenses/LICENSE,sha256=N1_nOvHTM6BobYnOTNXiQkroDqCEi6EzfGBv8lWtyZ0,1077
|
|
19
|
-
pico_ioc-1.4.0.dist-info/METADATA,sha256=DdvaybzEQOnC-HD563NJZqMDL7zwY3SnEpLGwjPxVzU,10346
|
|
20
|
-
pico_ioc-1.4.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
21
|
-
pico_ioc-1.4.0.dist-info/top_level.txt,sha256=_7_RLu616z_dtRw16impXn4Mw8IXe2J4BeX5912m5dQ,9
|
|
22
|
-
pico_ioc-1.4.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|