pico-ioc 2.0.4__py3-none-any.whl → 2.1.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pico-ioc
3
- Version: 2.0.4
3
+ Version: 2.1.0
4
4
  Summary: A minimalist, zero-dependency Inversion of Control (IoC) container for Python.
5
5
  Author-email: David Perez Cabrera <dperezcabrera@gmail.com>
6
6
  License: MIT License
@@ -68,13 +68,13 @@ It brings *Inversion of Control* and *dependency injection* to Python in a deter
68
68
 
69
69
  ## ⚖️ Core Principles
70
70
 
71
- - **Single Purpose** – Do one thing: dependency management.
72
- - **Declarative** – Use simple decorators (`@component`, `@factory`, `@configuration`) instead of config files or YAML magic.
73
- - **Deterministic** – No hidden scanning or side-effects; everything flows from an explicit `init()`.
74
- - **Async-Native** – Fully supports async providers, async lifecycle hooks, and async interceptors.
75
- - **Fail-Fast** – Detects missing bindings and circular dependencies at bootstrap.
76
- - **Testable by Design** – Use `overrides` and `profiles` to swap components instantly.
77
- - **Zero Core Dependencies** – Built entirely on the Python standard library. Optional features may require external packages (see Installation).
71
+ - **Single Purpose** – Do one thing: dependency management.
72
+ - **Declarative** – Use simple decorators (`@component`, `@factory`, `@provides`, `@configured`) instead of complex config files.
73
+ - **Deterministic** – No hidden scanning or side-effects; everything flows from an explicit `init()`.
74
+ - **Async-Native** – Fully supports async providers, async lifecycle hooks (`__ainit__`), and async interceptors.
75
+ - **Fail-Fast** – Detects missing bindings and circular dependencies at bootstrap (`init()`).
76
+ - **Testable by Design** – Use `overrides` and `profiles` to swap components instantly.
77
+ - **Zero Core Dependencies** – Built entirely on the Python standard library. Optional features may require external packages (see Installation).
78
78
 
79
79
  ---
80
80
 
@@ -83,25 +83,24 @@ It brings *Inversion of Control* and *dependency injection* to Python in a deter
83
83
  As Python systems evolve, wiring dependencies by hand becomes fragile and unmaintainable.
84
84
  **Pico-IoC** eliminates that friction by letting you declare how components relate — not how they’re created.
85
85
 
86
- | Feature | Manual Wiring | With Pico-IoC |
87
- | :------------- | :------------------------- | :------------------------------ |
88
- | Object creation| `svc = Service(Repo(Config()))` | `svc = container.get(Service)` |
89
- | Replacing deps | Monkey-patch | `overrides={Repo: FakeRepo()}` |
90
- | Coupling | Tight | Loose |
91
- | Testing | Painful | Instant |
92
- | Async support | Manual | Built-in |
86
+ | Feature | Manual Wiring | With Pico-IoC |
87
+ | :-------------- | :------------------------- | :-------------------------------- |
88
+ | Object creation | `svc = Service(Repo(Config()))` | `svc = container.get(Service)` |
89
+ | Replacing deps | Monkey-patch | `overrides={Repo: FakeRepo()}` |
90
+ | Coupling | Tight | Loose |
91
+ | Testing | Painful | Instant |
92
+ | Async support | Manual | Built-in (`aget`, `__ainit__`, ...) |
93
93
 
94
94
  ---
95
95
 
96
- ## 🧩 Highlights (v2.0.0)
96
+ ## 🧩 Highlights (v2.0+)
97
97
 
98
- - **Full redesign:** unified architecture with simpler, more powerful APIs.
99
- - **Async-aware AOP system** method interceptors via `@intercepted_by`.
100
- - **Typed configuration** dataclasses with JSON/YAML/env sources.
101
- - **Scoped resolution** singleton, prototype, request, session, transaction.
102
- - **UnifiedComponentProxy** transparent lazy/AOP proxy supporting serialization.
103
- - **Tree-based configuration runtime** with reusable adapters and discriminators.
104
- - **Observable container context** with stats, health checks, and async cleanup.
98
+ - **Unified Configuration:** Use `@configured` to bind both **flat** (ENV-like) and **tree** (YAML/JSON) sources via the `configuration(...)` builder (ADR-0010).
99
+ - **Async-aware AOP system:** Method interceptors via `@intercepted_by`.
100
+ - **Scoped resolution:** singleton, prototype, request, session, transaction, and custom scopes.
101
+ - **`UnifiedComponentProxy`:** Transparent `lazy=True` and AOP proxy supporting serialization.
102
+ - **Tree-based configuration runtime:** Advanced mapping with reusable adapters and discriminators (`Annotated[Union[...], Discriminator(...)]`).
103
+ - **Observable container context:** Built-in stats, health checks (`@health`), observer hooks (`ContainerObserver`), dependency graph export (`export_graph`), and async cleanup.
105
104
 
106
105
  ---
107
106
 
@@ -109,7 +108,7 @@ As Python systems evolve, wiring dependencies by hand becomes fragile and unmain
109
108
 
110
109
  ```bash
111
110
  pip install pico-ioc
112
- ```
111
+ ````
113
112
 
114
113
  For optional features, you can install extras:
115
114
 
@@ -121,50 +120,68 @@ For optional features, you can install extras:
121
120
 
122
121
  (Requires `PyYAML`)
123
122
 
124
- * **Dependency Graph Export:**
123
+ * **Dependency Graph Export (Rendering):**
125
124
 
126
125
  ```bash
127
- pip install pico-ioc[graphviz]
126
+ # You still need Graphviz command-line tools installed separately
127
+ # This extra is currently not required by the code,
128
+ # as export_graph generates the .dot file content directly.
129
+ # pip install pico-ioc[graphviz] # Consider removing if not used by code
128
130
  ```
129
131
 
130
- (Requires the `graphviz` Python package and the Graphviz command-line tools)
131
-
132
132
  -----
133
133
 
134
- ## ⚙️ Quick Example
134
+ ## ⚙️ Quick Example (Unified Configuration)
135
135
 
136
136
  ```python
137
+ import os
137
138
  from dataclasses import dataclass
138
- from pico_ioc import component, configuration, init
139
+ from pico_ioc import component, configured, configuration, init, EnvSource
139
140
 
140
- @configuration
141
+ # 1. Define configuration with @configured
142
+ @configured(prefix="APP_", mapping="auto") # Auto-detects flat mapping
141
143
  @dataclass
142
144
  class Config:
143
145
  db_url: str = "sqlite:///demo.db"
144
146
 
147
+ # 2. Define components
145
148
  @component
146
149
  class Repo:
147
- def __init__(self, cfg: Config):
150
+ def __init__(self, cfg: Config): # Inject config
148
151
  self.cfg = cfg
149
152
  def fetch(self):
150
153
  return f"fetching from {self.cfg.db_url}"
151
154
 
152
155
  @component
153
156
  class Service:
154
- def __init__(self, repo: Repo):
157
+ def __init__(self, repo: Repo): # Inject Repo
155
158
  self.repo = repo
156
159
  def run(self):
157
160
  return self.repo.fetch()
158
161
 
159
- container = init(modules=[__name__])
162
+ # --- Example Setup ---
163
+ os.environ['APP_DB_URL'] = 'postgresql://user:pass@host/db'
164
+
165
+ # 3. Build configuration context
166
+ config_ctx = configuration(
167
+ EnvSource(prefix="") # Read APP_DB_URL from environment
168
+ )
169
+
170
+ # 4. Initialize container
171
+ container = init(modules=[__name__], config=config_ctx) # Pass context via 'config'
172
+
173
+ # 5. Get and use the service
160
174
  svc = container.get(Service)
161
175
  print(svc.run())
176
+
177
+ # --- Cleanup ---
178
+ del os.environ['APP_DB_URL']
162
179
  ```
163
180
 
164
181
  **Output:**
165
182
 
166
183
  ```
167
- fetching from sqlite:///demo.db
184
+ fetching from postgresql://user:pass@host/db
168
185
  ```
169
186
 
170
187
  -----
@@ -175,7 +192,16 @@ fetching from sqlite:///demo.db
175
192
  class FakeRepo:
176
193
  def fetch(self): return "fake-data"
177
194
 
178
- container = init(modules=[__name__], overrides={Repo: FakeRepo()})
195
+ # Build configuration context (might be empty or specific for test)
196
+ test_config_ctx = configuration()
197
+
198
+ # Use overrides during init
199
+ container = init(
200
+ modules=[__name__],
201
+ config=test_config_ctx,
202
+ overrides={Repo: FakeRepo()} # Replace Repo with FakeRepo
203
+ )
204
+
179
205
  svc = container.get(Service)
180
206
  assert svc.run() == "fake-data"
181
207
  ```
@@ -185,23 +211,46 @@ assert svc.run() == "fake-data"
185
211
  ## 🩺 Lifecycle & AOP
186
212
 
187
213
  ```python
188
- from pico_ioc import intercepted_by, MethodInterceptor, MethodCtx
214
+ import time # For example
215
+ from pico_ioc import component, init, intercepted_by, MethodInterceptor, MethodCtx
189
216
 
217
+ # Define an interceptor component
218
+ @component
190
219
  class LogInterceptor(MethodInterceptor):
191
220
  def invoke(self, ctx: MethodCtx, call_next):
192
- print(f"→ calling {ctx.name}")
193
- res = call_next(ctx)
194
- print(f"← {ctx.name} done")
195
- return res
221
+ print(f"→ calling {ctx.cls.__name__}.{ctx.name}")
222
+ start = time.perf_counter()
223
+ try:
224
+ res = call_next(ctx)
225
+ duration = (time.perf_counter() - start) * 1000
226
+ print(f"← {ctx.cls.__name__}.{ctx.name} done ({duration:.2f}ms)")
227
+ return res
228
+ except Exception as e:
229
+ duration = (time.perf_counter() - start) * 1000
230
+ print(f"← {ctx.cls.__name__}.{ctx.name} failed ({duration:.2f}ms): {e}")
231
+ raise
196
232
 
197
233
  @component
198
234
  class Demo:
199
- @intercepted_by(LogInterceptor)
235
+ @intercepted_by(LogInterceptor) # Apply the interceptor
200
236
  def work(self):
237
+ print(" Working...")
238
+ time.sleep(0.01)
201
239
  return "ok"
202
240
 
241
+ # Initialize container (must scan module containing interceptor too)
203
242
  c = init(modules=[__name__])
204
- c.get(Demo).work()
243
+ result = c.get(Demo).work()
244
+ print(f"Result: {result}")
245
+ ```
246
+
247
+ **Output:**
248
+
249
+ ```
250
+ → calling Demo.work
251
+ Working...
252
+ ← Demo.work done (10.xxms)
253
+ Result: ok
205
254
  ```
206
255
 
207
256
  -----
@@ -233,7 +282,7 @@ tox
233
282
 
234
283
  ## 🧾 Changelog
235
284
 
236
- See [CHANGELOG.md](./CHANGELOG.md) — *Full redesign for v2.0.0.*
285
+ See [CHANGELOG.md](./CHANGELOG.md) — *Significant redesigns and features in v2.0+.*
237
286
 
238
287
  -----
239
288
 
@@ -0,0 +1,25 @@
1
+ pico_ioc/__init__.py,sha256=pxYvrunT9cFAXxGbow5TRlzRXDVvm4E7L6PT1UkPeUw,2394
2
+ pico_ioc/_version.py,sha256=LbU43-7hsLmdXWI0wTAl3y6D3Tr7CbJiDOLXwl7clj8,22
3
+ pico_ioc/analysis.py,sha256=k49R-HcDyvpSNid8mxv7Fc6fPHnDu1C_b4HxrGLNF2g,2780
4
+ pico_ioc/aop.py,sha256=r8JCPsTTF1hmrQMtnj-_Lj99fIy5J97RAJjiR2LjVPQ,12769
5
+ pico_ioc/api.py,sha256=tR0pm6YEnDTA62EIT_Cpw1EaPfdnBFY9Dvpc38ypP-o,5061
6
+ pico_ioc/component_scanner.py,sha256=S-9XNxrgyq_JFdc4Uqn2bEb-HxafSgIWylIurxyN_UA,7955
7
+ pico_ioc/config_builder.py,sha256=ROBvbnm2Zv8apRdtZJHtebp-2cOMiuENFyiqaX1T2Ik,2918
8
+ pico_ioc/config_registrar.py,sha256=8Xpl-CNRFny3EIRlcNx6oMJQldXkj_jdaPbC17qk2Ec,7803
9
+ pico_ioc/config_runtime.py,sha256=Q8jVFQ1b6h1ZOkdacm70u0Q7TlEiUGENFkG-YWRkBxA,12133
10
+ pico_ioc/constants.py,sha256=AhIt0ieDZ9Turxb_YbNzj11wUbBbzjKfWh1BDlSx2Nw,183
11
+ pico_ioc/container.py,sha256=1gRjHH5kq_lOlnGUh5QYA4vUHF3ScQpwXoT9PWiyJ2g,18352
12
+ pico_ioc/decorators.py,sha256=qrx4oMjRmmgwqT4cdp6FxBoqex3L1yNGlCUxO865z14,6347
13
+ pico_ioc/dependency_validator.py,sha256=BIR6pKntACiabF6CjNZ3m00RMnet9BPK1_9y1iCJ5KQ,4144
14
+ pico_ioc/event_bus.py,sha256=E8Qb8KZ6K1CuXSbMlG0MNPHkGoWlssLLPzHq1QYdADQ,8346
15
+ pico_ioc/exceptions.py,sha256=FBuajj5g29hAGODt2tAWuy2sG5mQojdSddaqFzim-aY,2383
16
+ pico_ioc/factory.py,sha256=oJXx_BYJuvV8oxYzs5I3gx9WM6uLYZ8GCc43gukNanc,1671
17
+ pico_ioc/locator.py,sha256=UDyXl3oSK-sZ8ouOlhsDM91shMP0atMxyMoQlaSYvfE,4885
18
+ pico_ioc/provider_selector.py,sha256=pU7NbI5vifvUlJEjlRJmvveQUZVD47T24QmiP0CHRw0,1213
19
+ pico_ioc/registrar.py,sha256=cexXFDsuxBwM8FvW7mwmuyqcTcOkSP6ELecwklcs0F4,7625
20
+ pico_ioc/scope.py,sha256=GDsDJWw7e5Vpiys-M4vQfKMJWSCiorRsT5cPo6z34Mk,5924
21
+ pico_ioc-2.1.0.dist-info/licenses/LICENSE,sha256=N1_nOvHTM6BobYnOTNXiQkroDqCEi6EzfGBv8lWtyZ0,1077
22
+ pico_ioc-2.1.0.dist-info/METADATA,sha256=lYR54Lhdwmn5tDXol2M5ReJxBsXpdZ9bH0KrcYOlq2s,10672
23
+ pico_ioc-2.1.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
24
+ pico_ioc-2.1.0.dist-info/top_level.txt,sha256=_7_RLu616z_dtRw16impXn4Mw8IXe2J4BeX5912m5dQ,9
25
+ pico_ioc-2.1.0.dist-info/RECORD,,
@@ -1,17 +0,0 @@
1
- pico_ioc/__init__.py,sha256=x7Sg0i4P0aXN3L5WH9IoTkyELPk-NQBdaRq8GuoZqO4,2294
2
- pico_ioc/_version.py,sha256=YuAuFLBrpl-SUl_UsvW1U1NTjqnldfuD1xUmSLmtaRw,22
3
- pico_ioc/aop.py,sha256=prFSlZC6vJYUfTbkMvlSc1T9UvvdEHr94Z0HAvjZ1fg,12985
4
- pico_ioc/api.py,sha256=vcmdU_7HHlsHR50N6230ICICIO-AhQ6M0xs2AzYMVVk,47476
5
- pico_ioc/config_runtime.py,sha256=z1cHDb5PbM8PMLYRFf5c2dmze8V22xwEzpWcBhtmMpA,11950
6
- pico_ioc/constants.py,sha256=AhIt0ieDZ9Turxb_YbNzj11wUbBbzjKfWh1BDlSx2Nw,183
7
- pico_ioc/container.py,sha256=-0o3T2uiKCnYVHTXP4MnUnA-atNeITjz0CtvUc_rY_E,17237
8
- pico_ioc/event_bus.py,sha256=E8Qb8KZ6K1CuXSbMlG0MNPHkGoWlssLLPzHq1QYdADQ,8346
9
- pico_ioc/exceptions.py,sha256=PnIY60r8O-eYXEVk7XPghz-hHNx_012ZLw9ikDwQhqQ,3019
10
- pico_ioc/factory.py,sha256=RMJJrD91HJdb7R3C6y48Fnia7YRQnQuMuYTC2cIAm34,1579
11
- pico_ioc/locator.py,sha256=PBxZYO_xCOxG7aJZ0adDtINrJass_ZDNYmPD2O_oNqM,2401
12
- pico_ioc/scope.py,sha256=GDsDJWw7e5Vpiys-M4vQfKMJWSCiorRsT5cPo6z34Mk,5924
13
- pico_ioc-2.0.4.dist-info/licenses/LICENSE,sha256=N1_nOvHTM6BobYnOTNXiQkroDqCEi6EzfGBv8lWtyZ0,1077
14
- pico_ioc-2.0.4.dist-info/METADATA,sha256=tvJtJVxclklqQcQ9_k4cB4VPtsog2tWfngm_-z8TEa4,8741
15
- pico_ioc-2.0.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
16
- pico_ioc-2.0.4.dist-info/top_level.txt,sha256=_7_RLu616z_dtRw16impXn4Mw8IXe2J4BeX5912m5dQ,9
17
- pico_ioc-2.0.4.dist-info/RECORD,,