modern-di-fastapi 0.5.0__tar.gz → 0.7.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: modern-di-fastapi
3
- Version: 0.5.0
3
+ Version: 0.7.0
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
@@ -1,19 +1,18 @@
1
1
  import contextlib
2
2
  import dataclasses
3
- import enum
4
3
  import typing
5
4
 
6
5
  import fastapi
7
6
  from fastapi.routing import _merge_lifespan_context
8
- from modern_di import Container, Scope, providers
7
+ from modern_di import AsyncContainer, Scope, providers
9
8
  from starlette.requests import HTTPConnection
10
9
 
11
10
 
12
11
  T_co = typing.TypeVar("T_co", covariant=True)
13
12
 
14
13
 
15
- def fetch_di_container(app_: fastapi.FastAPI) -> Container:
16
- return typing.cast(Container, app_.state.di_container)
14
+ def fetch_di_container(app_: fastapi.FastAPI) -> AsyncContainer:
15
+ return typing.cast(AsyncContainer, app_.state.di_container)
17
16
 
18
17
 
19
18
  @contextlib.asynccontextmanager
@@ -22,26 +21,28 @@ async def _lifespan_manager(app_: fastapi.FastAPI) -> typing.AsyncIterator[None]
22
21
  yield
23
22
 
24
23
 
25
- def setup_di(app: fastapi.FastAPI, scope: enum.IntEnum = Scope.APP) -> Container:
26
- app.state.di_container = Container(scope=scope)
24
+ def setup_di(app: fastapi.FastAPI, scope: Scope = Scope.APP, container: AsyncContainer | None = None) -> AsyncContainer:
25
+ if not container:
26
+ container = AsyncContainer(scope=scope)
27
+ app.state.di_container = container
27
28
  old_lifespan_manager = app.router.lifespan_context
28
29
  app.router.lifespan_context = _merge_lifespan_context(
29
30
  old_lifespan_manager,
30
31
  _lifespan_manager,
31
32
  )
32
- return app.state.di_container
33
+ return container
33
34
 
34
35
 
35
- async def build_di_container(connection: HTTPConnection) -> typing.AsyncIterator[Container]:
36
- context: dict[str, typing.Any] = {}
36
+ async def build_di_container(connection: HTTPConnection) -> typing.AsyncIterator[AsyncContainer]:
37
+ context: dict[type[typing.Any], typing.Any] = {}
37
38
  scope: Scope | None = None
38
39
  if isinstance(connection, fastapi.Request):
39
40
  scope = Scope.REQUEST
40
- context["request"] = connection
41
+ context[fastapi.Request] = connection
41
42
  elif isinstance(connection, fastapi.WebSocket):
42
- context["websocket"] = connection
43
+ context[fastapi.WebSocket] = connection
43
44
  scope = Scope.SESSION
44
- container: Container = fetch_di_container(connection.app)
45
+ container: AsyncContainer = fetch_di_container(connection.app)
45
46
  async with container.build_child_container(context=context, scope=scope) as request_container:
46
47
  yield request_container
47
48
 
@@ -51,9 +52,9 @@ class Dependency(typing.Generic[T_co]):
51
52
  dependency: providers.AbstractProvider[T_co]
52
53
 
53
54
  async def __call__(
54
- self, request_container: typing.Annotated[Container, fastapi.Depends(build_di_container)]
55
+ self, request_container: typing.Annotated[AsyncContainer, fastapi.Depends(build_di_container)]
55
56
  ) -> T_co:
56
- return await self.dependency.async_resolve(request_container)
57
+ return await request_container.resolve_provider(self.dependency)
57
58
 
58
59
 
59
60
  def FromDI(dependency: providers.AbstractProvider[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
@@ -15,7 +15,7 @@ classifiers = [
15
15
  "Topic :: Software Development :: Libraries",
16
16
  ]
17
17
  dependencies = ["fastapi>=0.100", "modern-di"]
18
- dynamic = ["version"]
18
+ version = "0.7.0"
19
19
 
20
20
  [project.urls]
21
21
  repository = "https://github.com/modern-python/modern-di"
@@ -33,14 +33,9 @@ dev = [
33
33
  ]
34
34
 
35
35
  [build-system]
36
- requires = ["hatchling", "hatch-vcs"]
36
+ requires = ["hatchling"]
37
37
  build-backend = "hatchling.build"
38
38
 
39
- [tool.hatch.version]
40
- source = "vcs"
41
- raw-options.root = "../.."
42
- fallback-version = "0"
43
-
44
39
  [tool.hatch.build]
45
40
  include = ["modern_di_fastapi"]
46
41