modern-di-fastapi 0.1.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.
- modern_di_fastapi-0.1.0/.gitignore +22 -0
- modern_di_fastapi-0.1.0/Justfile +23 -0
- modern_di_fastapi-0.1.0/PKG-INFO +26 -0
- modern_di_fastapi-0.1.0/README.md +6 -0
- modern_di_fastapi-0.1.0/modern_di_fastapi/__init__.py +9 -0
- modern_di_fastapi-0.1.0/modern_di_fastapi/depends.py +24 -0
- modern_di_fastapi-0.1.0/modern_di_fastapi/middleware.py +27 -0
- modern_di_fastapi-0.1.0/pyproject.toml +90 -0
- modern_di_fastapi-0.1.0/tests/__init__.py +0 -0
- modern_di_fastapi-0.1.0/tests/test_fastapi_di.py +69 -0
|
@@ -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,23 @@
|
|
|
1
|
+
default: install lint test
|
|
2
|
+
|
|
3
|
+
install:
|
|
4
|
+
uv lock --upgrade
|
|
5
|
+
uv sync --all-extras --frozen
|
|
6
|
+
|
|
7
|
+
lint:
|
|
8
|
+
uv run ruff format .
|
|
9
|
+
uv run ruff check . --fix
|
|
10
|
+
uv run mypy .
|
|
11
|
+
|
|
12
|
+
lint-ci:
|
|
13
|
+
uv run ruff format . --check
|
|
14
|
+
uv run ruff check . --no-fix
|
|
15
|
+
uv run mypy .
|
|
16
|
+
|
|
17
|
+
test *args:
|
|
18
|
+
uv run pytest {{ args }}
|
|
19
|
+
|
|
20
|
+
publish:
|
|
21
|
+
rm -rf dist
|
|
22
|
+
uv build
|
|
23
|
+
uv publish --token $PYPI_TOKEN
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: modern-di-fastapi
|
|
3
|
+
Version: 0.1.0
|
|
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
|
|
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,24 @@
|
|
|
1
|
+
import dataclasses
|
|
2
|
+
import typing
|
|
3
|
+
|
|
4
|
+
import fastapi
|
|
5
|
+
import modern_di
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
T_co = typing.TypeVar("T_co", covariant=True)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclasses.dataclass(slots=True, frozen=True)
|
|
12
|
+
class Dependency(typing.Generic[T_co]):
|
|
13
|
+
dependency: modern_di.resolvers.AbstractResolver[T_co]
|
|
14
|
+
|
|
15
|
+
async def __call__(self, request: fastapi.Request) -> T_co:
|
|
16
|
+
return await self.dependency.async_resolve(request.state.modern_di_container)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def FromDI(dependency: modern_di.resolvers.AbstractResolver[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
|
|
20
|
+
return typing.cast(T_co, fastapi.Depends(dependency=Dependency(dependency), use_cache=use_cache))
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def setup_modern_di(container: modern_di.Container, app: fastapi.FastAPI) -> None:
|
|
24
|
+
app.state.modern_di_container = container
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import typing
|
|
2
|
+
|
|
3
|
+
import modern_di
|
|
4
|
+
from starlette.requests import Request
|
|
5
|
+
from starlette.types import ASGIApp, Receive, Scope, Send
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ContainerMiddleware:
|
|
9
|
+
def __init__(self, app: ASGIApp) -> None:
|
|
10
|
+
self.app = app
|
|
11
|
+
|
|
12
|
+
async def __call__(
|
|
13
|
+
self,
|
|
14
|
+
scope: Scope,
|
|
15
|
+
receive: Receive,
|
|
16
|
+
send: Send,
|
|
17
|
+
) -> None:
|
|
18
|
+
if scope["type"] != "http":
|
|
19
|
+
return await self.app(scope, receive, send)
|
|
20
|
+
|
|
21
|
+
request = Request(scope, receive=receive, send=send)
|
|
22
|
+
context: dict[str, typing.Any] = {"request": request}
|
|
23
|
+
|
|
24
|
+
container: modern_di.Container = request.app.state.modern_di_container
|
|
25
|
+
async with container.build_child_container(context=context) as request_container:
|
|
26
|
+
request.state.modern_di_container = request_container
|
|
27
|
+
return await self.app(scope, receive, send)
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "modern-di-fastapi"
|
|
3
|
+
description = "Modern-DI integration for FastAPI"
|
|
4
|
+
authors = [
|
|
5
|
+
{ name = "Artur Shiriev", email = "me@shiriev.ru" },
|
|
6
|
+
]
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
requires-python = ">=3.10,<4"
|
|
9
|
+
license = "MIT"
|
|
10
|
+
keywords = ["DI", "dependency injector", "ioc-container", "FastAPI", "python"]
|
|
11
|
+
classifiers = [
|
|
12
|
+
"Programming Language :: Python :: 3.10",
|
|
13
|
+
"Programming Language :: Python :: 3.11",
|
|
14
|
+
"Programming Language :: Python :: 3.12",
|
|
15
|
+
"Programming Language :: Python :: 3.13",
|
|
16
|
+
"Typing :: Typed",
|
|
17
|
+
"Topic :: Software Development :: Libraries",
|
|
18
|
+
]
|
|
19
|
+
packages = [
|
|
20
|
+
{ include = "modern_di_fastapi" },
|
|
21
|
+
]
|
|
22
|
+
dynamic = ["version"]
|
|
23
|
+
dependencies = [
|
|
24
|
+
"fastapi>=0.100",
|
|
25
|
+
"modern-di",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[project.urls]
|
|
29
|
+
repository = "https://github.com/modern-python/modern-di"
|
|
30
|
+
docs = "https://modern-di.readthedocs.io"
|
|
31
|
+
|
|
32
|
+
[dependency-groups]
|
|
33
|
+
dev = [
|
|
34
|
+
"httpx",
|
|
35
|
+
"pytest",
|
|
36
|
+
"pytest-cov",
|
|
37
|
+
"pytest-asyncio",
|
|
38
|
+
"ruff",
|
|
39
|
+
"mypy",
|
|
40
|
+
"typing-extensions",
|
|
41
|
+
"asgi-lifespan",
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
[tool.uv.sources]
|
|
45
|
+
modern-di = { path = "../../" }
|
|
46
|
+
|
|
47
|
+
[build-system]
|
|
48
|
+
requires = ["hatchling", "hatch-vcs"]
|
|
49
|
+
build-backend = "hatchling.build"
|
|
50
|
+
|
|
51
|
+
[tool.hatch.version]
|
|
52
|
+
source = "vcs"
|
|
53
|
+
fallback-version = "0"
|
|
54
|
+
|
|
55
|
+
[tool.mypy]
|
|
56
|
+
python_version = "3.10"
|
|
57
|
+
strict = true
|
|
58
|
+
|
|
59
|
+
[tool.ruff]
|
|
60
|
+
fix = true
|
|
61
|
+
unsafe-fixes = true
|
|
62
|
+
line-length = 120
|
|
63
|
+
target-version = "py310"
|
|
64
|
+
extend-exclude = [
|
|
65
|
+
"docs",
|
|
66
|
+
]
|
|
67
|
+
|
|
68
|
+
[tool.ruff.lint]
|
|
69
|
+
select = ["ALL"]
|
|
70
|
+
ignore = [
|
|
71
|
+
"D1", # allow missing docstrings
|
|
72
|
+
"S101", # allow asserts
|
|
73
|
+
"TCH", # ignore flake8-type-checking
|
|
74
|
+
"FBT", # allow boolean args
|
|
75
|
+
"ANN101", # missing-type-self
|
|
76
|
+
"ANN102", # missing-type-cls
|
|
77
|
+
"D203", # "one-blank-line-before-class" conflicting with D211
|
|
78
|
+
"D213", # "multi-line-summary-second-line" conflicting with D212
|
|
79
|
+
"COM812", # flake8-commas "Trailing comma missing"
|
|
80
|
+
"ISC001", # flake8-implicit-str-concat
|
|
81
|
+
]
|
|
82
|
+
isort.lines-after-imports = 2
|
|
83
|
+
isort.no-lines-before = ["standard-library", "local-folder"]
|
|
84
|
+
|
|
85
|
+
[tool.pytest.ini_options]
|
|
86
|
+
addopts = "--cov=. --cov-report term-missing"
|
|
87
|
+
asyncio_mode = "auto"
|
|
88
|
+
|
|
89
|
+
[tool.coverage.report]
|
|
90
|
+
exclude_also = ["if typing.TYPE_CHECKING:"]
|
|
File without changes
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import contextlib
|
|
2
|
+
import dataclasses
|
|
3
|
+
import typing
|
|
4
|
+
|
|
5
|
+
import fastapi
|
|
6
|
+
import httpx
|
|
7
|
+
import modern_di
|
|
8
|
+
import pytest
|
|
9
|
+
from asgi_lifespan import LifespanManager
|
|
10
|
+
from modern_di import Scope, resolvers
|
|
11
|
+
from starlette import status
|
|
12
|
+
from starlette.requests import Request
|
|
13
|
+
|
|
14
|
+
import modern_di_fastapi
|
|
15
|
+
from modern_di_fastapi import ContainerMiddleware, FromDI
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@contextlib.asynccontextmanager
|
|
19
|
+
async def lifespan(app_: fastapi.FastAPI) -> typing.AsyncIterator[None]:
|
|
20
|
+
di_container = modern_di.Container(scope=modern_di.Scope.APP)
|
|
21
|
+
modern_di_fastapi.setup_modern_di(container=di_container, app=app_)
|
|
22
|
+
async with di_container:
|
|
23
|
+
yield
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
app = fastapi.FastAPI(lifespan=lifespan)
|
|
27
|
+
app.add_middleware(ContainerMiddleware)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@dataclasses.dataclass(kw_only=True, slots=True)
|
|
31
|
+
class SimpleCreator:
|
|
32
|
+
dep1: str
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclasses.dataclass(kw_only=True, slots=True)
|
|
36
|
+
class DependentCreator:
|
|
37
|
+
dep1: SimpleCreator
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def context_adapter_function(*, request: Request, **_: object) -> str:
|
|
41
|
+
return request.method
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
app_factory = resolvers.Factory(Scope.APP, SimpleCreator, dep1="original")
|
|
45
|
+
request_factory = resolvers.Factory(Scope.REQUEST, DependentCreator, dep1=app_factory.cast)
|
|
46
|
+
context_adapter = resolvers.ContextAdapter(Scope.REQUEST, context_adapter_function)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@app.get("/")
|
|
50
|
+
async def read_root(
|
|
51
|
+
app_factory_instance: typing.Annotated[SimpleCreator, FromDI(app_factory)],
|
|
52
|
+
request_factory_instance: typing.Annotated[DependentCreator, FromDI(request_factory)],
|
|
53
|
+
method: typing.Annotated[str, FromDI(context_adapter)],
|
|
54
|
+
) -> str:
|
|
55
|
+
assert isinstance(app_factory_instance, SimpleCreator)
|
|
56
|
+
assert isinstance(request_factory_instance, DependentCreator)
|
|
57
|
+
return method
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
@pytest.fixture(scope="session")
|
|
61
|
+
async def client() -> typing.AsyncIterator[httpx.AsyncClient]:
|
|
62
|
+
async with LifespanManager(app):
|
|
63
|
+
yield httpx.AsyncClient(app=app, base_url="http://test")
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
async def test_read_main(client: httpx.AsyncClient) -> None:
|
|
67
|
+
response = await client.get("/")
|
|
68
|
+
assert response.status_code == status.HTTP_200_OK
|
|
69
|
+
assert response.json() == "GET"
|