modern-di-fastapi 0.6.0__tar.gz → 1.0.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.6.0
3
+ Version: 1.0.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
@@ -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>=1.0.0alpha
19
20
  Description-Content-Type: text/markdown
20
21
 
21
22
  "Modern-DI-FastAPI"
@@ -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,9 +21,7 @@ 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: Container | None = None) -> Container:
26
- if not container:
27
- container = Container(scope=scope)
24
+ def setup_di(app: fastapi.FastAPI, container: AsyncContainer) -> AsyncContainer:
28
25
  app.state.di_container = container
29
26
  old_lifespan_manager = app.router.lifespan_context
30
27
  app.router.lifespan_context = _merge_lifespan_context(
@@ -34,29 +31,31 @@ def setup_di(app: fastapi.FastAPI, scope: enum.IntEnum = Scope.APP, container: C
34
31
  return container
35
32
 
36
33
 
37
- async def build_di_container(connection: HTTPConnection) -> typing.AsyncIterator[Container]:
38
- context: dict[str, typing.Any] = {}
34
+ async def build_di_container(connection: HTTPConnection) -> typing.AsyncIterator[AsyncContainer]:
35
+ context: dict[type[typing.Any], typing.Any] = {}
39
36
  scope: Scope | None = None
40
37
  if isinstance(connection, fastapi.Request):
41
38
  scope = Scope.REQUEST
42
- context["request"] = connection
39
+ context[fastapi.Request] = connection
43
40
  elif isinstance(connection, fastapi.WebSocket):
44
- context["websocket"] = connection
41
+ context[fastapi.WebSocket] = connection
45
42
  scope = Scope.SESSION
46
- container: Container = fetch_di_container(connection.app)
43
+ container: AsyncContainer = fetch_di_container(connection.app)
47
44
  async with container.build_child_container(context=context, scope=scope) as request_container:
48
45
  yield request_container
49
46
 
50
47
 
51
48
  @dataclasses.dataclass(slots=True, frozen=True)
52
49
  class Dependency(typing.Generic[T_co]):
53
- dependency: providers.AbstractProvider[T_co]
50
+ dependency: providers.AbstractProvider[T_co] | type[T_co]
54
51
 
55
52
  async def __call__(
56
- self, request_container: typing.Annotated[Container, fastapi.Depends(build_di_container)]
53
+ self, request_container: typing.Annotated[AsyncContainer, fastapi.Depends(build_di_container)]
57
54
  ) -> T_co:
58
- return await self.dependency.async_resolve(request_container)
55
+ if isinstance(self.dependency, providers.AbstractProvider):
56
+ return await request_container.resolve_provider(self.dependency)
57
+ return await request_container.resolve(dependency_type=self.dependency)
59
58
 
60
59
 
61
- def FromDI(dependency: providers.AbstractProvider[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
60
+ def FromDI(dependency: providers.AbstractProvider[T_co] | type[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
62
61
  return typing.cast(T_co, fastapi.Depends(dependency=Dependency(dependency), use_cache=use_cache))
@@ -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
- dynamic = ["version"]
18
+ dependencies = ["fastapi>=0.100", "modern-di>=1.0.0alpha"]
19
+ version = "1.0.0"
19
20
 
20
21
  [project.urls]
21
22
  repository = "https://github.com/modern-python/modern-di"
@@ -33,14 +34,9 @@ dev = [
33
34
  ]
34
35
 
35
36
  [build-system]
36
- requires = ["hatchling", "hatch-vcs"]
37
+ requires = ["hatchling"]
37
38
  build-backend = "hatchling.build"
38
39
 
39
- [tool.hatch.version]
40
- source = "vcs"
41
- raw-options.root = "../.."
42
- fallback-version = "0"
43
-
44
40
  [tool.hatch.build]
45
41
  include = ["modern_di_fastapi"]
46
42