pico-ioc 0.5.2__py3-none-any.whl → 1.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/scanner.py ADDED
@@ -0,0 +1,238 @@
1
+ # pico_ioc/scanner.py
2
+ import importlib
3
+ import inspect
4
+ import logging
5
+ import pkgutil
6
+ from types import ModuleType
7
+ from typing import Any, Callable, Optional, Tuple, List, Iterable
8
+
9
+ from .container import PicoContainer, Binder
10
+ from .decorators import (
11
+ COMPONENT_FLAG,
12
+ COMPONENT_KEY,
13
+ COMPONENT_LAZY,
14
+ FACTORY_FLAG,
15
+ PROVIDES_KEY,
16
+ PROVIDES_LAZY,
17
+ )
18
+ from .proxy import ComponentProxy
19
+ from .resolver import Resolver
20
+ from .plugins import PicoPlugin
21
+ from . import _state
22
+
23
+
24
+ def scan_and_configure(
25
+ package_or_name: Any,
26
+ container: PicoContainer,
27
+ *,
28
+ exclude: Optional[Callable[[str], bool]] = None,
29
+ plugins: Tuple[PicoPlugin, ...] = (),
30
+ ) -> None:
31
+ """
32
+ Scan a package, discover component classes/factories, and bind them into the container.
33
+
34
+ Args:
35
+ package_or_name: Package module or importable package name (str).
36
+ container: Target PicoContainer to receive bindings.
37
+ exclude: Optional predicate that receives a module name and returns True to skip it.
38
+ plugins: Optional lifecycle plugins that receive scan/bind events.
39
+ """
40
+ package = _as_module(package_or_name)
41
+ logging.info("Scanning in '%s'...", getattr(package, "__name__", repr(package)))
42
+
43
+ binder = Binder(container)
44
+ resolver = Resolver(container)
45
+
46
+ _run_plugin_hook(plugins, "before_scan", package, binder)
47
+
48
+ comp_classes, factory_classes = _collect_decorated_classes(
49
+ package=package,
50
+ exclude=exclude,
51
+ plugins=plugins,
52
+ binder=binder,
53
+ )
54
+
55
+ _run_plugin_hook(plugins, "after_scan", package, binder)
56
+
57
+ _register_component_classes(
58
+ classes=comp_classes,
59
+ container=container,
60
+ resolver=resolver,
61
+ )
62
+
63
+ _register_factory_classes(
64
+ factory_classes=factory_classes,
65
+ container=container,
66
+ resolver=resolver,
67
+ )
68
+
69
+
70
+ # -------------------- Helpers (private) --------------------
71
+
72
+ def _as_module(package_or_name: Any) -> ModuleType:
73
+ """Return a module from either a module object or an importable string name."""
74
+ if isinstance(package_or_name, str):
75
+ return importlib.import_module(package_or_name)
76
+ if hasattr(package_or_name, "__spec__"):
77
+ return package_or_name # type: ignore[return-value]
78
+ raise TypeError("package_or_name must be a module or importable package name (str).")
79
+
80
+
81
+ def _run_plugin_hook(
82
+ plugins: Tuple[PicoPlugin, ...],
83
+ hook_name: str,
84
+ *args,
85
+ **kwargs,
86
+ ) -> None:
87
+ """Run a lifecycle hook across all plugins, logging (but not raising) exceptions."""
88
+ for pl in plugins:
89
+ try:
90
+ fn = getattr(pl, hook_name, None)
91
+ if fn:
92
+ fn(*args, **kwargs)
93
+ except Exception:
94
+ logging.exception("Plugin %s failed", hook_name)
95
+
96
+
97
+ def _iter_package_modules(
98
+ package: ModuleType,
99
+ ) -> Iterable[str]:
100
+ """
101
+ Yield fully qualified module names under the given package.
102
+
103
+ Requires the package to have a __path__ (i.e., be a package, not a single module).
104
+ """
105
+ try:
106
+ pkg_path = package.__path__ # type: ignore[attr-defined]
107
+ except Exception:
108
+ return # not a package; nothing to iterate
109
+
110
+ prefix = package.__name__ + "."
111
+ for _finder, name, _is_pkg in pkgutil.walk_packages(pkg_path, prefix):
112
+ yield name
113
+
114
+
115
+ def _collect_decorated_classes(
116
+ *,
117
+ package: ModuleType,
118
+ exclude: Optional[Callable[[str], bool]],
119
+ plugins: Tuple[PicoPlugin, ...],
120
+ binder: Binder,
121
+ ) -> Tuple[List[type], List[type]]:
122
+ """
123
+ Import modules under `package`, visit classes, and collect those marked with
124
+ @component or @factory_component decorators.
125
+ """
126
+ comp_classes: List[type] = []
127
+ factory_classes: List[type] = []
128
+
129
+ def _visit_module(module: ModuleType):
130
+ for _name, obj in inspect.getmembers(module, inspect.isclass):
131
+ # Allow plugins to inspect/transform/record classes
132
+ _run_plugin_hook(plugins, "visit_class", module, obj, binder)
133
+
134
+ # Collect decorated classes
135
+ if getattr(obj, COMPONENT_FLAG, False):
136
+ comp_classes.append(obj)
137
+ elif getattr(obj, FACTORY_FLAG, False):
138
+ factory_classes.append(obj)
139
+
140
+ # 1) Si es un paquete, recorrer submódulos
141
+ for mod_name in _iter_package_modules(package):
142
+ if exclude and exclude(mod_name):
143
+ logging.info("Skipping module %s (excluded)", mod_name)
144
+ continue
145
+
146
+ try:
147
+ module = importlib.import_module(mod_name)
148
+ except Exception as e:
149
+ logging.warning("Module %s not processed: %s", mod_name, e)
150
+ continue
151
+
152
+ _visit_module(module)
153
+
154
+ # 2) Si el “paquete” raíz es un módulo (sin __path__), también hay que visitarlo.
155
+ if not hasattr(package, "__path__"):
156
+ _visit_module(package)
157
+
158
+ return comp_classes, factory_classes
159
+
160
+
161
+ def _register_component_classes(
162
+ *,
163
+ classes: List[type],
164
+ container: PicoContainer,
165
+ resolver: Resolver,
166
+ ) -> None:
167
+ """
168
+ Register @component classes into the container.
169
+
170
+ Binding key:
171
+ - If the class has COMPONENT_KEY, use it; otherwise, bind by the class itself.
172
+ Laziness:
173
+ - If COMPONENT_LAZY is True, provide a proxy that defers instantiation.
174
+ """
175
+ for cls in classes:
176
+ key = getattr(cls, COMPONENT_KEY, cls)
177
+ is_lazy = bool(getattr(cls, COMPONENT_LAZY, False))
178
+
179
+ def _provider_factory(c=cls, lazy=is_lazy):
180
+ def _factory():
181
+ if lazy:
182
+ return ComponentProxy(lambda: resolver.create_instance(c))
183
+ return resolver.create_instance(c)
184
+ return _factory
185
+
186
+ container.bind(key, _provider_factory(), lazy=is_lazy)
187
+
188
+
189
+ def _register_factory_classes(
190
+ *,
191
+ factory_classes: List[type],
192
+ container: PicoContainer,
193
+ resolver: Resolver,
194
+ ) -> None:
195
+ """
196
+ Register products of @factory_component classes.
197
+
198
+ For each factory class:
199
+ - Instantiate the factory via the resolver.
200
+ - For each method with @provides:
201
+ - Bind the provided key to a callable that calls the factory method.
202
+ - If PROVIDES_LAZY is True, bind a proxy that defers the method call.
203
+ """
204
+ for fcls in factory_classes:
205
+ try:
206
+ # Durante el escaneo, permitir la resolución de dependencias de la factory
207
+ # elevando temporalmente el flag `_resolving` para no chocar con la guardia.
208
+ tok_res = _state._resolving.set(True)
209
+ try:
210
+ finst = resolver.create_instance(fcls)
211
+ finally:
212
+ _state._resolving.reset(tok_res)
213
+ except Exception:
214
+ logging.exception("Error in factory %s", fcls.__name__)
215
+ continue
216
+
217
+ for attr_name, func in inspect.getmembers(fcls, predicate=inspect.isfunction):
218
+ provided_key = getattr(func, PROVIDES_KEY, None)
219
+ if provided_key is None:
220
+ continue
221
+
222
+ is_lazy = bool(getattr(func, PROVIDES_LAZY, False))
223
+ # `bound` is the bound method on the instance (so it has `self`)
224
+ bound = getattr(finst, attr_name, func.__get__(finst, fcls))
225
+
226
+ def _make_provider(m=bound, owner=fcls, lazy=is_lazy):
227
+ def _factory():
228
+ # Compute kwargs at call time to ensure up-to-date dependency resolution
229
+ kwargs = resolver.kwargs_for_callable(m, owner_cls=owner)
230
+
231
+ def _call():
232
+ return m(**kwargs)
233
+
234
+ return ComponentProxy(lambda: _call()) if lazy else _call()
235
+ return _factory
236
+
237
+ container.bind(provided_key, _make_provider(), lazy=is_lazy)
238
+
@@ -0,0 +1,29 @@
1
+ # pico_ioc/typing_utils.py
2
+
3
+ import sys
4
+ import typing
5
+
6
+
7
+ def evaluated_hints(func, owner_cls=None) -> dict:
8
+ """Return type hints; swallow any error and return {}."""
9
+ try:
10
+ module = sys.modules.get(func.__module__)
11
+ globalns = getattr(module, "__dict__", {})
12
+ localns = vars(owner_cls) if owner_cls is not None else None
13
+ return typing.get_type_hints(func, globalns=globalns, localns=localns, include_extras=True)
14
+ except Exception:
15
+ return {}
16
+
17
+
18
+ def resolve_annotation_to_type(ann, func, owner_cls=None):
19
+ """Best-effort evaluation of a string annotation; return original on failure."""
20
+ if not isinstance(ann, str):
21
+ return ann
22
+ try:
23
+ module = sys.modules.get(func.__module__)
24
+ globalns = getattr(module, "__dict__", {})
25
+ localns = vars(owner_cls) if owner_cls is not None else None
26
+ return eval(ann, globalns, localns)
27
+ except Exception:
28
+ return ann
29
+
@@ -0,0 +1,145 @@
1
+ Metadata-Version: 2.4
2
+ Name: pico-ioc
3
+ Version: 1.0.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
+ [![PyPI](https://img.shields.io/pypi/v/pico-ioc.svg)](https://pypi.org/project/pico-ioc/)
49
+ [![DeepWiki](https://img.shields.io/badge/docs-DeepWiki-blue)](https://deepwiki.com/dperezcabrera/pico-ioc)
50
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
51
+ ![CI (tox matrix)](https://github.com/dperezcabrera/pico-ioc/actions/workflows/ci.yml/badge.svg)
52
+ [![codecov](https://codecov.io/gh/dperezcabrera/pico-ioc/branch/main/graph/badge.svg)](https://codecov.io/gh/dperezcabrera/pico-ioc)
53
+ [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=dperezcabrera_pico-ioc&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=dperezcabrera_pico-ioc)
54
+ [![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=dperezcabrera_pico-ioc&metric=duplicated_lines_density)](https://sonarcloud.io/summary/new_code?id=dperezcabrera_pico-ioc)
55
+ [![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=dperezcabrera_pico-ioc&metric=sqale_rating)](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
+ ## ✨ Features
65
+
66
+ - **Zero dependencies** — pure Python, framework-agnostic.
67
+ - **Decorator API** — `@component`, `@factory_component`, `@provides`, `@plugin`.
68
+ - **Fail-fast bootstrap** — eager by default; missing deps surface at startup.
69
+ - **Opt-in lazy** — `lazy=True` wraps with `ComponentProxy`.
70
+ - **Smart resolution order** — parameter name → type annotation → MRO → string.
71
+ - **Qualifiers & collections** — `list[Annotated[T, Q]]` filters by qualifier.
72
+ - **Plugins** — lifecycle hooks (`before_scan`, `after_ready`).
73
+ - **Public API helper** — auto-export decorated symbols in `__init__.py`.
74
+ - **Thread/async safe** — isolation via `ContextVar`.
75
+
76
+ ---
77
+
78
+ ## 📦 Installation
79
+
80
+ ```bash
81
+ # Requires Python 3.10+
82
+ pip install pico-ioc
83
+ ````
84
+
85
+ ---
86
+
87
+ ## 🚀 Quick start
88
+
89
+ ```python
90
+ from pico_ioc import component, init
91
+
92
+ @component
93
+ class Config:
94
+ url = "sqlite:///demo.db"
95
+
96
+ @component
97
+ class Repo:
98
+ def __init__(self, cfg: Config):
99
+ self.url = cfg.url
100
+ def fetch(self): return f"fetching from {self.url}"
101
+
102
+ @component
103
+ class Service:
104
+ def __init__(self, repo: Repo):
105
+ self.repo = repo
106
+ def run(self): return self.repo.fetch()
107
+
108
+ # bootstrap
109
+ import myapp
110
+ c = init(myapp)
111
+ svc = c.get(Service)
112
+ print(svc.run())
113
+ ```
114
+
115
+ **Output:**
116
+
117
+ ```
118
+ fetching from sqlite:///demo.db
119
+ ```
120
+
121
+ ---
122
+
123
+ ## 📖 Documentation
124
+
125
+ * [Overview](.llm/OVERVIEW.md) — mission & concepts
126
+ * [Guide](.llm/GUIDE.md) — practical usage & recipes
127
+ * [Architecture](.llm/ARCHITECTURE.md) — internals & design rationale
128
+
129
+ ---
130
+
131
+ ## 🧪 Development
132
+
133
+ ```bash
134
+ pip install tox
135
+ tox
136
+ ```
137
+
138
+ ---
139
+
140
+ ## 📜 License
141
+
142
+ MIT — see [LICENSE](https://opensource.org/licenses/MIT)
143
+
144
+
145
+
@@ -0,0 +1,17 @@
1
+ pico_ioc/__init__.py,sha256=7EyifZ02LFfhkAR6zzAaQJvByyMI2_-qNrw49gPoJkA,633
2
+ pico_ioc/_state.py,sha256=KHNtdPrv1s-uynfot2IsNkWotBPyORPCcM2xe9qMMPo,286
3
+ pico_ioc/_version.py,sha256=RsZjRjMprNcDm97wqRRSk6rTLgTX8N0GyicZyZ8OsBQ,22
4
+ pico_ioc/api.py,sha256=pvzAx725UG5Z6TPqQqiWdfytb0ddk26-cxnkTJRaK8o,3648
5
+ pico_ioc/container.py,sha256=81Jys4Ipu57B4pM5xQuq48S5oEhuKin7Y8vEERg-4_4,5085
6
+ pico_ioc/decorators.py,sha256=gNPxLFNEdFUFYlxILKjU76XyoPohediK0DNiv-RpK3I,1902
7
+ pico_ioc/plugins.py,sha256=JbI-28VLGJaik7ysXi3L-YGTGxhqwJH4W5QYuWSruDE,589
8
+ pico_ioc/proxy.py,sha256=-e3Z9z7Bc_2wxswwUJI_s8AfvCTps8f8RWUJ9RuEp7E,4606
9
+ pico_ioc/public_api.py,sha256=E3sArCoI1xxkIw7xQBvLYAWcIoVJjcq1s0kH-0qIVDE,2383
10
+ pico_ioc/resolver.py,sha256=PN5uq2dFEStAzzTEl0n4AEyC-k7TowqCu7-uwIDDgEk,3897
11
+ pico_ioc/scanner.py,sha256=-iOj_qX15IBFmOAFQlP_1rIkBfamGzdqYdvIPOvq7sY,7709
12
+ pico_ioc/typing_utils.py,sha256=JQ4bkR60pKxFs3f8JlEz41ruKDsWj-SmkKv3DLJriec,950
13
+ pico_ioc-1.0.0.dist-info/licenses/LICENSE,sha256=N1_nOvHTM6BobYnOTNXiQkroDqCEi6EzfGBv8lWtyZ0,1077
14
+ pico_ioc-1.0.0.dist-info/METADATA,sha256=CZqjp3KYScMMGV25CGrAnDVj37ZbGw3BD9eWC9tjOxw,5434
15
+ pico_ioc-1.0.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
16
+ pico_ioc-1.0.0.dist-info/top_level.txt,sha256=_7_RLu616z_dtRw16impXn4Mw8IXe2J4BeX5912m5dQ,9
17
+ pico_ioc-1.0.0.dist-info/RECORD,,