modern-di-fastapi 1.0.0a0__tar.gz → 2.1.1__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.
- {modern_di_fastapi-1.0.0a0 → modern_di_fastapi-2.1.1}/PKG-INFO +3 -2
- {modern_di_fastapi-1.0.0a0 → modern_di_fastapi-2.1.1}/modern_di_fastapi/main.py +22 -12
- {modern_di_fastapi-1.0.0a0 → modern_di_fastapi-2.1.1}/pyproject.toml +3 -2
- {modern_di_fastapi-1.0.0a0 → modern_di_fastapi-2.1.1}/.gitignore +0 -0
- {modern_di_fastapi-1.0.0a0 → modern_di_fastapi-2.1.1}/README.md +0 -0
- {modern_di_fastapi-1.0.0a0 → modern_di_fastapi-2.1.1}/modern_di_fastapi/__init__.py +0 -0
- {modern_di_fastapi-1.0.0a0 → modern_di_fastapi-2.1.1}/modern_di_fastapi/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: modern-di-fastapi
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 2.1.1
|
|
4
4
|
Summary: Modern-DI integration for FastAPI
|
|
5
5
|
Project-URL: repository, https://github.com/modern-python/modern-di
|
|
6
6
|
Project-URL: docs, https://modern-di.readthedocs.io
|
|
@@ -11,11 +11,12 @@ Classifier: Programming Language :: Python :: 3.10
|
|
|
11
11
|
Classifier: Programming Language :: Python :: 3.11
|
|
12
12
|
Classifier: Programming Language :: Python :: 3.12
|
|
13
13
|
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
14
15
|
Classifier: Topic :: Software Development :: Libraries
|
|
15
16
|
Classifier: Typing :: Typed
|
|
16
17
|
Requires-Python: <4,>=3.10
|
|
17
18
|
Requires-Dist: fastapi>=0.100
|
|
18
|
-
Requires-Dist: modern-di>=
|
|
19
|
+
Requires-Dist: modern-di>=2
|
|
19
20
|
Description-Content-Type: text/markdown
|
|
20
21
|
|
|
21
22
|
"Modern-DI-FastAPI"
|
|
@@ -4,25 +4,33 @@ import typing
|
|
|
4
4
|
|
|
5
5
|
import fastapi
|
|
6
6
|
from fastapi.routing import _merge_lifespan_context
|
|
7
|
-
from modern_di import
|
|
7
|
+
from modern_di import Container, Scope, providers
|
|
8
8
|
from starlette.requests import HTTPConnection
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
T_co = typing.TypeVar("T_co", covariant=True)
|
|
12
12
|
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
fastapi_request = providers.ContextProvider(scope=Scope.REQUEST, context_type=fastapi.Request)
|
|
15
|
+
fastapi_websocket = providers.ContextProvider(scope=Scope.SESSION, context_type=fastapi.WebSocket)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def fetch_di_container(app_: fastapi.FastAPI) -> Container:
|
|
19
|
+
return typing.cast(Container, app_.state.di_container)
|
|
16
20
|
|
|
17
21
|
|
|
18
22
|
@contextlib.asynccontextmanager
|
|
19
23
|
async def _lifespan_manager(app_: fastapi.FastAPI) -> typing.AsyncIterator[None]:
|
|
20
|
-
|
|
24
|
+
container = fetch_di_container(app_)
|
|
25
|
+
try:
|
|
21
26
|
yield
|
|
27
|
+
finally:
|
|
28
|
+
await container.close_async()
|
|
22
29
|
|
|
23
30
|
|
|
24
|
-
def setup_di(app: fastapi.FastAPI, container:
|
|
31
|
+
def setup_di(app: fastapi.FastAPI, container: Container) -> Container:
|
|
25
32
|
app.state.di_container = container
|
|
33
|
+
container.providers_registry.add_providers(fastapi_request=fastapi_request, fastapi_websocket=fastapi_websocket)
|
|
26
34
|
old_lifespan_manager = app.router.lifespan_context
|
|
27
35
|
app.router.lifespan_context = _merge_lifespan_context(
|
|
28
36
|
old_lifespan_manager,
|
|
@@ -31,7 +39,7 @@ def setup_di(app: fastapi.FastAPI, container: AsyncContainer) -> AsyncContainer:
|
|
|
31
39
|
return container
|
|
32
40
|
|
|
33
41
|
|
|
34
|
-
async def build_di_container(connection: HTTPConnection) -> typing.AsyncIterator[
|
|
42
|
+
async def build_di_container(connection: HTTPConnection) -> typing.AsyncIterator[Container]:
|
|
35
43
|
context: dict[type[typing.Any], typing.Any] = {}
|
|
36
44
|
scope: Scope | None = None
|
|
37
45
|
if isinstance(connection, fastapi.Request):
|
|
@@ -40,9 +48,11 @@ async def build_di_container(connection: HTTPConnection) -> typing.AsyncIterator
|
|
|
40
48
|
elif isinstance(connection, fastapi.WebSocket):
|
|
41
49
|
context[fastapi.WebSocket] = connection
|
|
42
50
|
scope = Scope.SESSION
|
|
43
|
-
container
|
|
44
|
-
|
|
45
|
-
yield
|
|
51
|
+
container = fetch_di_container(connection.app).build_child_container(context=context, scope=scope)
|
|
52
|
+
try:
|
|
53
|
+
yield container
|
|
54
|
+
finally:
|
|
55
|
+
await container.close_async()
|
|
46
56
|
|
|
47
57
|
|
|
48
58
|
@dataclasses.dataclass(slots=True, frozen=True)
|
|
@@ -50,11 +60,11 @@ class Dependency(typing.Generic[T_co]):
|
|
|
50
60
|
dependency: providers.AbstractProvider[T_co] | type[T_co]
|
|
51
61
|
|
|
52
62
|
async def __call__(
|
|
53
|
-
self, request_container: typing.Annotated[
|
|
63
|
+
self, request_container: typing.Annotated[Container, fastapi.Depends(build_di_container)]
|
|
54
64
|
) -> T_co:
|
|
55
65
|
if isinstance(self.dependency, providers.AbstractProvider):
|
|
56
|
-
return
|
|
57
|
-
return
|
|
66
|
+
return request_container.resolve_provider(self.dependency)
|
|
67
|
+
return request_container.resolve(dependency_type=self.dependency)
|
|
58
68
|
|
|
59
69
|
|
|
60
70
|
def FromDI(dependency: providers.AbstractProvider[T_co] | type[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
|
|
@@ -11,11 +11,12 @@ classifiers = [
|
|
|
11
11
|
"Programming Language :: Python :: 3.11",
|
|
12
12
|
"Programming Language :: Python :: 3.12",
|
|
13
13
|
"Programming Language :: Python :: 3.13",
|
|
14
|
+
"Programming Language :: Python :: 3.14",
|
|
14
15
|
"Typing :: Typed",
|
|
15
16
|
"Topic :: Software Development :: Libraries",
|
|
16
17
|
]
|
|
17
|
-
dependencies = ["fastapi>=0.100", "modern-di>=
|
|
18
|
-
version = "1.
|
|
18
|
+
dependencies = ["fastapi>=0.100", "modern-di>=2"]
|
|
19
|
+
version = "2.1.1"
|
|
19
20
|
|
|
20
21
|
[project.urls]
|
|
21
22
|
repository = "https://github.com/modern-python/modern-di"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|