anydi 0.56.0__tar.gz → 0.58.0__tar.gz
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.
- {anydi-0.56.0 → anydi-0.58.0}/PKG-INFO +32 -13
- {anydi-0.56.0 → anydi-0.58.0}/README.md +31 -12
- {anydi-0.56.0 → anydi-0.58.0}/anydi/__init__.py +4 -2
- {anydi-0.56.0 → anydi-0.58.0}/anydi/_container.py +181 -157
- anydi-0.58.0/anydi/_injector.py +132 -0
- {anydi-0.56.0 → anydi-0.58.0}/anydi/_resolver.py +51 -24
- anydi-0.58.0/anydi/_scanner.py +118 -0
- anydi-0.58.0/anydi/_types.py +113 -0
- {anydi-0.56.0 → anydi-0.58.0}/anydi/ext/fastapi.py +31 -33
- {anydi-0.56.0 → anydi-0.58.0}/anydi/ext/faststream.py +25 -31
- {anydi-0.56.0 → anydi-0.58.0}/anydi/ext/pydantic_settings.py +2 -1
- anydi-0.58.0/anydi/ext/pytest_plugin.py +477 -0
- {anydi-0.56.0 → anydi-0.58.0}/anydi/ext/starlette/middleware.py +1 -1
- {anydi-0.56.0 → anydi-0.58.0}/pyproject.toml +6 -1
- anydi-0.56.0/anydi/_scanner.py +0 -110
- anydi-0.56.0/anydi/_types.py +0 -72
- anydi-0.56.0/anydi/ext/pytest_plugin.py +0 -147
- {anydi-0.56.0 → anydi-0.58.0}/anydi/_async_lock.py +0 -0
- {anydi-0.56.0 → anydi-0.58.0}/anydi/_context.py +0 -0
- {anydi-0.56.0 → anydi-0.58.0}/anydi/_decorators.py +0 -0
- {anydi-0.56.0 → anydi-0.58.0}/anydi/_module.py +0 -0
- {anydi-0.56.0 → anydi-0.58.0}/anydi/_provider.py +0 -0
- {anydi-0.56.0 → anydi-0.58.0}/anydi/ext/__init__.py +0 -0
- {anydi-0.56.0 → anydi-0.58.0}/anydi/ext/django/__init__.py +0 -0
- {anydi-0.56.0 → anydi-0.58.0}/anydi/ext/starlette/__init__.py +0 -0
- {anydi-0.56.0 → anydi-0.58.0}/anydi/py.typed +0 -0
- {anydi-0.56.0 → anydi-0.58.0}/anydi/testing.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: anydi
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.58.0
|
|
4
4
|
Summary: Dependency Injection library
|
|
5
5
|
Keywords: dependency injection,dependencies,di,async,asyncio,application
|
|
6
6
|
Author: Anton Ruhlov
|
|
@@ -58,9 +58,9 @@ The key features are:
|
|
|
58
58
|
|
|
59
59
|
* **Type-safe**: Dependency resolution is driven by type hints.
|
|
60
60
|
* **Async-ready**: Works the same for sync and async providers or injections.
|
|
61
|
-
* **Scoped**: Built-in singleton, transient, and request
|
|
61
|
+
* **Scoped**: Built-in singleton, transient, and request scopes, plus custom scopes.
|
|
62
62
|
* **Simple**: Small surface area keeps boilerplate low.
|
|
63
|
-
* **Fast**:
|
|
63
|
+
* **Fast**: Has minimal overhead and resolves dependencies quickly.
|
|
64
64
|
* **Named**: `Annotated[...]` makes multiple bindings per type simple.
|
|
65
65
|
* **Managed**: Providers can open/close resources via context managers.
|
|
66
66
|
* **Modular**: Compose containers or modules for large apps.
|
|
@@ -74,7 +74,7 @@ The key features are:
|
|
|
74
74
|
pip install anydi
|
|
75
75
|
```
|
|
76
76
|
|
|
77
|
-
##
|
|
77
|
+
## Quick Example
|
|
78
78
|
|
|
79
79
|
### Define a Service (`app/services.py`)
|
|
80
80
|
|
|
@@ -116,19 +116,18 @@ if __name__ == "__main__":
|
|
|
116
116
|
### Inject Into Functions (`app/main.py`)
|
|
117
117
|
|
|
118
118
|
```python
|
|
119
|
-
from anydi import
|
|
119
|
+
from anydi import Provide
|
|
120
120
|
|
|
121
121
|
from app.container import container
|
|
122
122
|
from app.services import GreetingService
|
|
123
123
|
|
|
124
124
|
|
|
125
|
-
|
|
126
|
-
def greet(service: GreetingService = Inject()) -> str:
|
|
125
|
+
def greet(service: Provide[GreetingService]) -> str:
|
|
127
126
|
return service.greet("World")
|
|
128
127
|
|
|
129
128
|
|
|
130
129
|
if __name__ == "__main__":
|
|
131
|
-
print(greet
|
|
130
|
+
print(container.run(greet))
|
|
132
131
|
```
|
|
133
132
|
|
|
134
133
|
### Test with Overrides (`tests/test_app.py`)
|
|
@@ -146,7 +145,7 @@ def test_greet() -> None:
|
|
|
146
145
|
service_mock.greet.return_value = "Mocked"
|
|
147
146
|
|
|
148
147
|
with container.override(GreetingService, service_mock):
|
|
149
|
-
result = greet
|
|
148
|
+
result = container.run(greet)
|
|
150
149
|
|
|
151
150
|
assert result == "Mocked"
|
|
152
151
|
```
|
|
@@ -158,8 +157,8 @@ from typing import Annotated
|
|
|
158
157
|
|
|
159
158
|
import anydi.ext.fastapi
|
|
160
159
|
from fastapi import FastAPI
|
|
161
|
-
from anydi.ext.fastapi import Inject
|
|
162
160
|
|
|
161
|
+
from anydi import Provide
|
|
163
162
|
from app.container import container
|
|
164
163
|
from app.services import GreetingService
|
|
165
164
|
|
|
@@ -169,7 +168,7 @@ app = FastAPI()
|
|
|
169
168
|
|
|
170
169
|
@app.get("/greeting")
|
|
171
170
|
async def greet(
|
|
172
|
-
service:
|
|
171
|
+
service: Provide[GreetingService]
|
|
173
172
|
) -> dict[str, str]:
|
|
174
173
|
return {"greeting": service.greet("World")}
|
|
175
174
|
|
|
@@ -245,7 +244,7 @@ Wire Django Ninja (`urls.py`):
|
|
|
245
244
|
```python
|
|
246
245
|
from typing import Annotated, Any
|
|
247
246
|
|
|
248
|
-
from anydi import
|
|
247
|
+
from anydi import Provide
|
|
249
248
|
from django.http import HttpRequest
|
|
250
249
|
from django.urls import path
|
|
251
250
|
from ninja import NinjaAPI
|
|
@@ -257,7 +256,7 @@ api = NinjaAPI()
|
|
|
257
256
|
|
|
258
257
|
|
|
259
258
|
@api.get("/greeting")
|
|
260
|
-
def greet(request: HttpRequest, service:
|
|
259
|
+
def greet(request: HttpRequest, service: Provide[GreetingService]) -> Any:
|
|
261
260
|
return {"greeting": service.greet("World")}
|
|
262
261
|
|
|
263
262
|
|
|
@@ -265,3 +264,23 @@ urlpatterns = [
|
|
|
265
264
|
path("api/", api.urls),
|
|
266
265
|
]
|
|
267
266
|
```
|
|
267
|
+
|
|
268
|
+
## What's Next?
|
|
269
|
+
|
|
270
|
+
Ready to learn more? Check out these resources:
|
|
271
|
+
|
|
272
|
+
**Core Documentation:**
|
|
273
|
+
- [Core Concepts](https://anydi.readthedocs.io/en/latest/concepts/) - Understand containers, providers, scopes, and dependency injection
|
|
274
|
+
- [Providers](https://anydi.readthedocs.io/en/latest/usage/providers/) - Learn about registration, named providers, and resource management
|
|
275
|
+
- [Scopes](https://anydi.readthedocs.io/en/latest/usage/scopes/) - Master lifecycle management with built-in and custom scopes
|
|
276
|
+
- [Dependency Injection](https://anydi.readthedocs.io/en/latest/usage/injection/) - Explore injection patterns and techniques
|
|
277
|
+
- [Testing](https://anydi.readthedocs.io/en/latest/usage/testing/) - Write testable code with provider overrides
|
|
278
|
+
|
|
279
|
+
**Framework Integrations:**
|
|
280
|
+
- [FastAPI](https://anydi.readthedocs.io/en/latest/extensions/fastapi/) - Build modern APIs with automatic dependency injection
|
|
281
|
+
- [Django](https://anydi.readthedocs.io/en/latest/extensions/django/) - Integrate with Django and Django Ninja
|
|
282
|
+
- [FastStream](https://anydi.readthedocs.io/en/latest/extensions/faststream/) - Message broker applications
|
|
283
|
+
- [Pydantic Settings](https://anydi.readthedocs.io/en/latest/extensions/pydantic_settings/) - Configuration management
|
|
284
|
+
|
|
285
|
+
**Full Documentation:**
|
|
286
|
+
- [Read the Docs](https://anydi.readthedocs.io/) - Complete documentation with examples and guides
|
|
@@ -23,9 +23,9 @@ The key features are:
|
|
|
23
23
|
|
|
24
24
|
* **Type-safe**: Dependency resolution is driven by type hints.
|
|
25
25
|
* **Async-ready**: Works the same for sync and async providers or injections.
|
|
26
|
-
* **Scoped**: Built-in singleton, transient, and request
|
|
26
|
+
* **Scoped**: Built-in singleton, transient, and request scopes, plus custom scopes.
|
|
27
27
|
* **Simple**: Small surface area keeps boilerplate low.
|
|
28
|
-
* **Fast**:
|
|
28
|
+
* **Fast**: Has minimal overhead and resolves dependencies quickly.
|
|
29
29
|
* **Named**: `Annotated[...]` makes multiple bindings per type simple.
|
|
30
30
|
* **Managed**: Providers can open/close resources via context managers.
|
|
31
31
|
* **Modular**: Compose containers or modules for large apps.
|
|
@@ -39,7 +39,7 @@ The key features are:
|
|
|
39
39
|
pip install anydi
|
|
40
40
|
```
|
|
41
41
|
|
|
42
|
-
##
|
|
42
|
+
## Quick Example
|
|
43
43
|
|
|
44
44
|
### Define a Service (`app/services.py`)
|
|
45
45
|
|
|
@@ -81,19 +81,18 @@ if __name__ == "__main__":
|
|
|
81
81
|
### Inject Into Functions (`app/main.py`)
|
|
82
82
|
|
|
83
83
|
```python
|
|
84
|
-
from anydi import
|
|
84
|
+
from anydi import Provide
|
|
85
85
|
|
|
86
86
|
from app.container import container
|
|
87
87
|
from app.services import GreetingService
|
|
88
88
|
|
|
89
89
|
|
|
90
|
-
|
|
91
|
-
def greet(service: GreetingService = Inject()) -> str:
|
|
90
|
+
def greet(service: Provide[GreetingService]) -> str:
|
|
92
91
|
return service.greet("World")
|
|
93
92
|
|
|
94
93
|
|
|
95
94
|
if __name__ == "__main__":
|
|
96
|
-
print(greet
|
|
95
|
+
print(container.run(greet))
|
|
97
96
|
```
|
|
98
97
|
|
|
99
98
|
### Test with Overrides (`tests/test_app.py`)
|
|
@@ -111,7 +110,7 @@ def test_greet() -> None:
|
|
|
111
110
|
service_mock.greet.return_value = "Mocked"
|
|
112
111
|
|
|
113
112
|
with container.override(GreetingService, service_mock):
|
|
114
|
-
result = greet
|
|
113
|
+
result = container.run(greet)
|
|
115
114
|
|
|
116
115
|
assert result == "Mocked"
|
|
117
116
|
```
|
|
@@ -123,8 +122,8 @@ from typing import Annotated
|
|
|
123
122
|
|
|
124
123
|
import anydi.ext.fastapi
|
|
125
124
|
from fastapi import FastAPI
|
|
126
|
-
from anydi.ext.fastapi import Inject
|
|
127
125
|
|
|
126
|
+
from anydi import Provide
|
|
128
127
|
from app.container import container
|
|
129
128
|
from app.services import GreetingService
|
|
130
129
|
|
|
@@ -134,7 +133,7 @@ app = FastAPI()
|
|
|
134
133
|
|
|
135
134
|
@app.get("/greeting")
|
|
136
135
|
async def greet(
|
|
137
|
-
service:
|
|
136
|
+
service: Provide[GreetingService]
|
|
138
137
|
) -> dict[str, str]:
|
|
139
138
|
return {"greeting": service.greet("World")}
|
|
140
139
|
|
|
@@ -210,7 +209,7 @@ Wire Django Ninja (`urls.py`):
|
|
|
210
209
|
```python
|
|
211
210
|
from typing import Annotated, Any
|
|
212
211
|
|
|
213
|
-
from anydi import
|
|
212
|
+
from anydi import Provide
|
|
214
213
|
from django.http import HttpRequest
|
|
215
214
|
from django.urls import path
|
|
216
215
|
from ninja import NinjaAPI
|
|
@@ -222,7 +221,7 @@ api = NinjaAPI()
|
|
|
222
221
|
|
|
223
222
|
|
|
224
223
|
@api.get("/greeting")
|
|
225
|
-
def greet(request: HttpRequest, service:
|
|
224
|
+
def greet(request: HttpRequest, service: Provide[GreetingService]) -> Any:
|
|
226
225
|
return {"greeting": service.greet("World")}
|
|
227
226
|
|
|
228
227
|
|
|
@@ -230,3 +229,23 @@ urlpatterns = [
|
|
|
230
229
|
path("api/", api.urls),
|
|
231
230
|
]
|
|
232
231
|
```
|
|
232
|
+
|
|
233
|
+
## What's Next?
|
|
234
|
+
|
|
235
|
+
Ready to learn more? Check out these resources:
|
|
236
|
+
|
|
237
|
+
**Core Documentation:**
|
|
238
|
+
- [Core Concepts](https://anydi.readthedocs.io/en/latest/concepts/) - Understand containers, providers, scopes, and dependency injection
|
|
239
|
+
- [Providers](https://anydi.readthedocs.io/en/latest/usage/providers/) - Learn about registration, named providers, and resource management
|
|
240
|
+
- [Scopes](https://anydi.readthedocs.io/en/latest/usage/scopes/) - Master lifecycle management with built-in and custom scopes
|
|
241
|
+
- [Dependency Injection](https://anydi.readthedocs.io/en/latest/usage/injection/) - Explore injection patterns and techniques
|
|
242
|
+
- [Testing](https://anydi.readthedocs.io/en/latest/usage/testing/) - Write testable code with provider overrides
|
|
243
|
+
|
|
244
|
+
**Framework Integrations:**
|
|
245
|
+
- [FastAPI](https://anydi.readthedocs.io/en/latest/extensions/fastapi/) - Build modern APIs with automatic dependency injection
|
|
246
|
+
- [Django](https://anydi.readthedocs.io/en/latest/extensions/django/) - Integrate with Django and Django Ninja
|
|
247
|
+
- [FastStream](https://anydi.readthedocs.io/en/latest/extensions/faststream/) - Message broker applications
|
|
248
|
+
- [Pydantic Settings](https://anydi.readthedocs.io/en/latest/extensions/pydantic_settings/) - Configuration management
|
|
249
|
+
|
|
250
|
+
**Full Documentation:**
|
|
251
|
+
- [Read the Docs](https://anydi.readthedocs.io/) - Complete documentation with examples and guides
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"""AnyDI public objects and functions."""
|
|
2
2
|
|
|
3
|
-
from ._container import Container
|
|
3
|
+
from ._container import Container, import_container
|
|
4
4
|
from ._decorators import injectable, provided, provider, request, singleton, transient
|
|
5
5
|
from ._module import Module
|
|
6
6
|
from ._provider import ProviderDef as Provider
|
|
7
|
-
from ._types import Inject, Scope
|
|
7
|
+
from ._types import Inject, Provide, Scope
|
|
8
8
|
|
|
9
9
|
# Alias for dependency auto marker
|
|
10
10
|
# TODO: deprecate it
|
|
@@ -15,9 +15,11 @@ __all__ = [
|
|
|
15
15
|
"Container",
|
|
16
16
|
"Inject",
|
|
17
17
|
"Module",
|
|
18
|
+
"Provide",
|
|
18
19
|
"Provider",
|
|
19
20
|
"Scope",
|
|
20
21
|
"auto",
|
|
22
|
+
"import_container",
|
|
21
23
|
"injectable",
|
|
22
24
|
"provided",
|
|
23
25
|
"provider",
|