modern-di-fastapi 0.4.1__tar.gz → 0.5.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
- Metadata-Version: 2.3
1
+ Metadata-Version: 2.4
2
2
  Name: modern-di-fastapi
3
- Version: 0.4.1
3
+ Version: 0.5.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
@@ -0,0 +1,9 @@
1
+ from modern_di_fastapi.main import FromDI, build_di_container, fetch_di_container, setup_di
2
+
3
+
4
+ __all__ = [
5
+ "FromDI",
6
+ "build_di_container",
7
+ "fetch_di_container",
8
+ "setup_di",
9
+ ]
@@ -1,8 +1,10 @@
1
+ import contextlib
1
2
  import dataclasses
2
3
  import enum
3
4
  import typing
4
5
 
5
6
  import fastapi
7
+ from fastapi.routing import _merge_lifespan_context
6
8
  from modern_di import Container, Scope, providers
7
9
  from starlette.requests import HTTPConnection
8
10
 
@@ -10,15 +12,26 @@ from starlette.requests import HTTPConnection
10
12
  T_co = typing.TypeVar("T_co", covariant=True)
11
13
 
12
14
 
15
+ def fetch_di_container(app_: fastapi.FastAPI) -> Container:
16
+ return typing.cast(Container, app_.state.di_container)
17
+
18
+
19
+ @contextlib.asynccontextmanager
20
+ async def _lifespan_manager(app_: fastapi.FastAPI) -> typing.AsyncIterator[None]:
21
+ async with fetch_di_container(app_):
22
+ yield
23
+
24
+
13
25
  def setup_di(app: fastapi.FastAPI, scope: enum.IntEnum = Scope.APP) -> Container:
14
26
  app.state.di_container = Container(scope=scope)
27
+ old_lifespan_manager = app.router.lifespan_context
28
+ app.router.lifespan_context = _merge_lifespan_context(
29
+ old_lifespan_manager,
30
+ _lifespan_manager,
31
+ )
15
32
  return app.state.di_container
16
33
 
17
34
 
18
- def fetch_di_container(app: fastapi.FastAPI) -> Container:
19
- return typing.cast(Container, app.state.di_container)
20
-
21
-
22
35
  async def build_di_container(connection: HTTPConnection) -> typing.AsyncIterator[Container]:
23
36
  context: dict[str, typing.Any] = {}
24
37
  scope: Scope | None = None
@@ -43,5 +56,5 @@ class Dependency(typing.Generic[T_co]):
43
56
  return await self.dependency.async_resolve(request_container)
44
57
 
45
58
 
46
- def Provide(dependency: providers.AbstractProvider[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
59
+ def FromDI(dependency: providers.AbstractProvider[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
47
60
  return typing.cast(T_co, fastapi.Depends(dependency=Dependency(dependency), use_cache=use_cache))
@@ -22,7 +22,15 @@ repository = "https://github.com/modern-python/modern-di"
22
22
  docs = "https://modern-di.readthedocs.io"
23
23
 
24
24
  [dependency-groups]
25
- dev = ["httpx", "asgi-lifespan"]
25
+ dev = [
26
+ "pytest",
27
+ "pytest-cov",
28
+ "pytest-asyncio",
29
+ "ruff",
30
+ "mypy",
31
+ "typing-extensions",
32
+ "httpx",
33
+ ]
26
34
 
27
35
  [build-system]
28
36
  requires = ["hatchling", "hatch-vcs"]
@@ -39,6 +47,7 @@ include = ["modern_di_fastapi"]
39
47
  [tool.pytest.ini_options]
40
48
  addopts = "--cov=. --cov-report term-missing"
41
49
  asyncio_mode = "auto"
50
+ asyncio_default_fixture_loop_scope = "function"
42
51
 
43
52
  [tool.coverage.report]
44
53
  exclude_also = ["if typing.TYPE_CHECKING:"]
@@ -1,9 +0,0 @@
1
- from modern_di_fastapi.main import Provide, build_di_container, fetch_di_container, setup_di
2
-
3
-
4
- __all__ = [
5
- "Provide",
6
- "build_di_container",
7
- "fetch_di_container",
8
- "setup_di",
9
- ]