modern-di-fastapi 1.0.0a0__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.
@@ -0,0 +1,22 @@
1
+ # Generic things
2
+ *.pyc
3
+ *~
4
+ __pycache__/*
5
+ *.swp
6
+ *.sqlite3
7
+ *.map
8
+ .vscode
9
+ .idea
10
+ .DS_Store
11
+ .env
12
+ .mypy_cache
13
+ .pytest_cache
14
+ .ruff_cache
15
+ .coverage
16
+ htmlcov/
17
+ coverage.xml
18
+ pytest.xml
19
+ dist/
20
+ .python-version
21
+ .venv
22
+ uv.lock
@@ -0,0 +1,26 @@
1
+ Metadata-Version: 2.4
2
+ Name: modern-di-fastapi
3
+ Version: 1.0.0a0
4
+ Summary: Modern-DI integration for FastAPI
5
+ Project-URL: repository, https://github.com/modern-python/modern-di
6
+ Project-URL: docs, https://modern-di.readthedocs.io
7
+ Author-email: Artur Shiriev <me@shiriev.ru>
8
+ License-Expression: MIT
9
+ Keywords: DI,FastAPI,dependency injector,ioc-container,python
10
+ Classifier: Programming Language :: Python :: 3.10
11
+ Classifier: Programming Language :: Python :: 3.11
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Classifier: Programming Language :: Python :: 3.13
14
+ Classifier: Topic :: Software Development :: Libraries
15
+ Classifier: Typing :: Typed
16
+ Requires-Python: <4,>=3.10
17
+ Requires-Dist: fastapi>=0.100
18
+ Requires-Dist: modern-di>=1
19
+ Description-Content-Type: text/markdown
20
+
21
+ "Modern-DI-FastAPI"
22
+ ==
23
+
24
+ Integration of [Modern-DI](https://github.com/modern-python/modern-di) to FastAPI
25
+
26
+ 📚 [Documentation](https://modern-di.readthedocs.io)
@@ -0,0 +1,6 @@
1
+ "Modern-DI-FastAPI"
2
+ ==
3
+
4
+ Integration of [Modern-DI](https://github.com/modern-python/modern-di) to FastAPI
5
+
6
+ 📚 [Documentation](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
+ ]
@@ -0,0 +1,61 @@
1
+ import contextlib
2
+ import dataclasses
3
+ import typing
4
+
5
+ import fastapi
6
+ from fastapi.routing import _merge_lifespan_context
7
+ from modern_di import AsyncContainer, Scope, providers
8
+ from starlette.requests import HTTPConnection
9
+
10
+
11
+ T_co = typing.TypeVar("T_co", covariant=True)
12
+
13
+
14
+ def fetch_di_container(app_: fastapi.FastAPI) -> AsyncContainer:
15
+ return typing.cast(AsyncContainer, app_.state.di_container)
16
+
17
+
18
+ @contextlib.asynccontextmanager
19
+ async def _lifespan_manager(app_: fastapi.FastAPI) -> typing.AsyncIterator[None]:
20
+ async with fetch_di_container(app_):
21
+ yield
22
+
23
+
24
+ def setup_di(app: fastapi.FastAPI, container: AsyncContainer) -> AsyncContainer:
25
+ app.state.di_container = container
26
+ old_lifespan_manager = app.router.lifespan_context
27
+ app.router.lifespan_context = _merge_lifespan_context(
28
+ old_lifespan_manager,
29
+ _lifespan_manager,
30
+ )
31
+ return container
32
+
33
+
34
+ async def build_di_container(connection: HTTPConnection) -> typing.AsyncIterator[AsyncContainer]:
35
+ context: dict[type[typing.Any], typing.Any] = {}
36
+ scope: Scope | None = None
37
+ if isinstance(connection, fastapi.Request):
38
+ scope = Scope.REQUEST
39
+ context[fastapi.Request] = connection
40
+ elif isinstance(connection, fastapi.WebSocket):
41
+ context[fastapi.WebSocket] = connection
42
+ scope = Scope.SESSION
43
+ container: AsyncContainer = fetch_di_container(connection.app)
44
+ async with container.build_child_container(context=context, scope=scope) as request_container:
45
+ yield request_container
46
+
47
+
48
+ @dataclasses.dataclass(slots=True, frozen=True)
49
+ class Dependency(typing.Generic[T_co]):
50
+ dependency: providers.AbstractProvider[T_co] | type[T_co]
51
+
52
+ async def __call__(
53
+ self, request_container: typing.Annotated[AsyncContainer, fastapi.Depends(build_di_container)]
54
+ ) -> T_co:
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)
58
+
59
+
60
+ def FromDI(dependency: providers.AbstractProvider[T_co] | type[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
61
+ return typing.cast(T_co, fastapi.Depends(dependency=Dependency(dependency), use_cache=use_cache))
File without changes
@@ -0,0 +1,48 @@
1
+ [project]
2
+ name = "modern-di-fastapi"
3
+ description = "Modern-DI integration for FastAPI"
4
+ authors = [{ name = "Artur Shiriev", email = "me@shiriev.ru" }]
5
+ requires-python = ">=3.10,<4"
6
+ license = "MIT"
7
+ readme = "README.md"
8
+ keywords = ["DI", "dependency injector", "ioc-container", "FastAPI", "python"]
9
+ classifiers = [
10
+ "Programming Language :: Python :: 3.10",
11
+ "Programming Language :: Python :: 3.11",
12
+ "Programming Language :: Python :: 3.12",
13
+ "Programming Language :: Python :: 3.13",
14
+ "Typing :: Typed",
15
+ "Topic :: Software Development :: Libraries",
16
+ ]
17
+ dependencies = ["fastapi>=0.100", "modern-di>=1"]
18
+ version = "1.0.0a0"
19
+
20
+ [project.urls]
21
+ repository = "https://github.com/modern-python/modern-di"
22
+ docs = "https://modern-di.readthedocs.io"
23
+
24
+ [dependency-groups]
25
+ dev = [
26
+ "pytest",
27
+ "pytest-cov",
28
+ "pytest-asyncio",
29
+ "ruff",
30
+ "mypy",
31
+ "typing-extensions",
32
+ "httpx",
33
+ ]
34
+
35
+ [build-system]
36
+ requires = ["hatchling"]
37
+ build-backend = "hatchling.build"
38
+
39
+ [tool.hatch.build]
40
+ include = ["modern_di_fastapi"]
41
+
42
+ [tool.pytest.ini_options]
43
+ addopts = "--cov=. --cov-report term-missing"
44
+ asyncio_mode = "auto"
45
+ asyncio_default_fixture_loop_scope = "function"
46
+
47
+ [tool.coverage.report]
48
+ exclude_also = ["if typing.TYPE_CHECKING:"]