modern-di-fastapi 0.4.0__tar.gz → 0.4.2__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.3
2
2
  Name: modern-di-fastapi
3
- Version: 0.4.0
3
+ Version: 0.4.2
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
+ ]
@@ -43,5 +43,5 @@ class Dependency(typing.Generic[T_co]):
43
43
  return await self.dependency.async_resolve(request_container)
44
44
 
45
45
 
46
- def Provide(dependency: providers.AbstractProvider[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
46
+ def FromDI(dependency: providers.AbstractProvider[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
47
47
  return typing.cast(T_co, fastapi.Depends(dependency=Dependency(dependency), use_cache=use_cache))
@@ -1,12 +1,10 @@
1
1
  [project]
2
2
  name = "modern-di-fastapi"
3
3
  description = "Modern-DI integration for FastAPI"
4
- authors = [
5
- { name = "Artur Shiriev", email = "me@shiriev.ru" },
6
- ]
7
- readme = "README.md"
4
+ authors = [{ name = "Artur Shiriev", email = "me@shiriev.ru" }]
8
5
  requires-python = ">=3.10,<4"
9
6
  license = "MIT"
7
+ readme = "README.md"
10
8
  keywords = ["DI", "dependency injector", "ioc-container", "FastAPI", "python"]
11
9
  classifiers = [
12
10
  "Programming Language :: Python :: 3.10",
@@ -16,14 +14,8 @@ classifiers = [
16
14
  "Typing :: Typed",
17
15
  "Topic :: Software Development :: Libraries",
18
16
  ]
19
- packages = [
20
- { include = "modern_di_fastapi" },
21
- ]
17
+ dependencies = ["fastapi>=0.100", "modern-di"]
22
18
  dynamic = ["version"]
23
- dependencies = [
24
- "fastapi>=0.100",
25
- "modern-di",
26
- ]
27
19
 
28
20
  [project.urls]
29
21
  repository = "https://github.com/modern-python/modern-di"
@@ -31,13 +23,13 @@ docs = "https://modern-di.readthedocs.io"
31
23
 
32
24
  [dependency-groups]
33
25
  dev = [
34
- "httpx",
35
26
  "pytest",
36
27
  "pytest-cov",
37
28
  "pytest-asyncio",
38
29
  "ruff",
39
30
  "mypy",
40
31
  "typing-extensions",
32
+ "httpx",
41
33
  "asgi-lifespan",
42
34
  ]
43
35
 
@@ -47,41 +39,16 @@ build-backend = "hatchling.build"
47
39
 
48
40
  [tool.hatch.version]
49
41
  source = "vcs"
42
+ raw-options.root = "../.."
50
43
  fallback-version = "0"
51
44
 
52
- [tool.mypy]
53
- python_version = "3.10"
54
- strict = true
55
-
56
- [tool.ruff]
57
- fix = true
58
- unsafe-fixes = true
59
- line-length = 120
60
- target-version = "py310"
61
- extend-exclude = [
62
- "docs",
63
- ]
64
-
65
- [tool.ruff.lint]
66
- select = ["ALL"]
67
- ignore = [
68
- "D1", # allow missing docstrings
69
- "S101", # allow asserts
70
- "TCH", # ignore flake8-type-checking
71
- "FBT", # allow boolean args
72
- "ANN101", # missing-type-self
73
- "ANN102", # missing-type-cls
74
- "D203", # "one-blank-line-before-class" conflicting with D211
75
- "D213", # "multi-line-summary-second-line" conflicting with D212
76
- "COM812", # flake8-commas "Trailing comma missing"
77
- "ISC001", # flake8-implicit-str-concat
78
- ]
79
- isort.lines-after-imports = 2
80
- isort.no-lines-before = ["standard-library", "local-folder"]
45
+ [tool.hatch.build]
46
+ include = ["modern_di_fastapi"]
81
47
 
82
48
  [tool.pytest.ini_options]
83
49
  addopts = "--cov=. --cov-report term-missing"
84
50
  asyncio_mode = "auto"
51
+ asyncio_default_fixture_loop_scope = "function"
85
52
 
86
53
  [tool.coverage.report]
87
54
  exclude_also = ["if typing.TYPE_CHECKING:"]
@@ -1,18 +0,0 @@
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 }}
@@ -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
- ]
File without changes
@@ -1,28 +0,0 @@
1
- import contextlib
2
- import typing
3
-
4
- import fastapi
5
- import pytest
6
- from asgi_lifespan import LifespanManager
7
- from starlette.testclient import TestClient
8
-
9
- from modern_di_fastapi import setup_di
10
-
11
-
12
- @contextlib.asynccontextmanager
13
- async def lifespan(app_: fastapi.FastAPI) -> typing.AsyncIterator[None]:
14
- container = setup_di(app_)
15
- async with container:
16
- yield
17
-
18
-
19
- @pytest.fixture
20
- async def app() -> typing.AsyncIterator[fastapi.FastAPI]:
21
- app_ = fastapi.FastAPI(lifespan=lifespan)
22
- async with LifespanManager(app_):
23
- yield app_
24
-
25
-
26
- @pytest.fixture
27
- def client(app: fastapi.FastAPI) -> TestClient:
28
- return TestClient(app=app)
@@ -1,11 +0,0 @@
1
- import dataclasses
2
-
3
-
4
- @dataclasses.dataclass(kw_only=True, slots=True)
5
- class SimpleCreator:
6
- dep1: str
7
-
8
-
9
- @dataclasses.dataclass(kw_only=True, slots=True)
10
- class DependentCreator:
11
- dep1: SimpleCreator
@@ -1,61 +0,0 @@
1
- import typing
2
-
3
- import fastapi
4
- import modern_di
5
- from modern_di import Scope, providers
6
- from starlette import status
7
- from starlette.testclient import TestClient
8
-
9
- from modern_di_fastapi import Provide, build_di_container
10
- from tests.dependencies import DependentCreator, SimpleCreator
11
-
12
-
13
- def context_adapter_function(*, request: fastapi.Request, **_: object) -> str:
14
- return request.method
15
-
16
-
17
- app_factory = providers.Factory(Scope.APP, SimpleCreator, dep1="original")
18
- request_factory = providers.Factory(Scope.REQUEST, DependentCreator, dep1=app_factory.cast)
19
- action_factory = providers.Factory(Scope.ACTION, DependentCreator, dep1=app_factory.cast)
20
- context_adapter = providers.ContextAdapter(Scope.REQUEST, context_adapter_function)
21
-
22
-
23
- def test_factories(client: TestClient, app: fastapi.FastAPI) -> None:
24
- @app.get("/")
25
- async def read_root(
26
- app_factory_instance: typing.Annotated[SimpleCreator, Provide(app_factory)],
27
- request_factory_instance: typing.Annotated[DependentCreator, Provide(request_factory)],
28
- ) -> None:
29
- assert isinstance(app_factory_instance, SimpleCreator)
30
- assert isinstance(request_factory_instance, DependentCreator)
31
- assert request_factory_instance.dep1 is not app_factory_instance
32
-
33
- response = client.get("/")
34
- assert response.status_code == status.HTTP_200_OK
35
- assert response.json() is None
36
-
37
-
38
- def test_context_adapter(client: TestClient, app: fastapi.FastAPI) -> None:
39
- @app.get("/")
40
- async def read_root(
41
- method: typing.Annotated[str, Provide(context_adapter)],
42
- ) -> None:
43
- assert method == "GET"
44
-
45
- response = client.get("/")
46
- assert response.status_code == status.HTTP_200_OK
47
- assert response.json() is None
48
-
49
-
50
- def test_factories_action_scope(client: TestClient, app: fastapi.FastAPI) -> None:
51
- @app.get("/")
52
- async def read_root(
53
- request_container: typing.Annotated[modern_di.Container, fastapi.Depends(build_di_container)],
54
- ) -> None:
55
- with request_container.build_child_container() as action_container:
56
- action_factory_instance = action_factory.sync_resolve(action_container)
57
- assert isinstance(action_factory_instance, DependentCreator)
58
-
59
- response = client.get("/")
60
- assert response.status_code == status.HTTP_200_OK
61
- assert response.json() is None
@@ -1,75 +0,0 @@
1
- import typing
2
-
3
- import fastapi
4
- import modern_di
5
- from modern_di import Scope, providers
6
- from starlette.testclient import TestClient
7
-
8
- from modern_di_fastapi import Provide, build_di_container
9
- from tests.dependencies import DependentCreator, SimpleCreator
10
-
11
-
12
- def context_adapter_function(*, websocket: fastapi.WebSocket, **_: object) -> str:
13
- return str(websocket.scope["path"])
14
-
15
-
16
- app_factory = providers.Factory(Scope.APP, SimpleCreator, dep1="original")
17
- session_factory = providers.Factory(Scope.SESSION, DependentCreator, dep1=app_factory.cast)
18
- request_factory = providers.Factory(Scope.REQUEST, DependentCreator, dep1=app_factory.cast)
19
- context_adapter = providers.ContextAdapter(Scope.SESSION, context_adapter_function)
20
-
21
-
22
- async def test_factories(client: TestClient, app: fastapi.FastAPI) -> None:
23
- @app.websocket("/ws")
24
- async def websocket_endpoint(
25
- websocket: fastapi.WebSocket,
26
- app_factory_instance: typing.Annotated[SimpleCreator, Provide(app_factory)],
27
- session_factory_instance: typing.Annotated[DependentCreator, Provide(session_factory)],
28
- ) -> None:
29
- assert isinstance(app_factory_instance, SimpleCreator)
30
- assert isinstance(session_factory_instance, DependentCreator)
31
- assert session_factory_instance.dep1 is not app_factory_instance
32
-
33
- await websocket.accept()
34
- await websocket.send_text("test")
35
- await websocket.close()
36
-
37
- with client.websocket_connect("/ws") as websocket:
38
- data = websocket.receive_text()
39
- assert data == "test"
40
-
41
-
42
- async def test_factories_request_scope(client: TestClient, app: fastapi.FastAPI) -> None:
43
- @app.websocket("/ws")
44
- async def websocket_endpoint(
45
- websocket: fastapi.WebSocket,
46
- session_container: typing.Annotated[modern_di.Container, fastapi.Depends(build_di_container)],
47
- ) -> None:
48
- with session_container.build_child_container() as request_container:
49
- request_factory_instance = request_factory.sync_resolve(request_container)
50
- assert isinstance(request_factory_instance, DependentCreator)
51
-
52
- await websocket.accept()
53
- await websocket.send_text("test")
54
- await websocket.close()
55
-
56
- with client.websocket_connect("/ws") as websocket:
57
- data = websocket.receive_text()
58
- assert data == "test"
59
-
60
-
61
- async def test_context_adapter(client: TestClient, app: fastapi.FastAPI) -> None:
62
- @app.websocket("/ws")
63
- async def websocket_endpoint(
64
- websocket: fastapi.WebSocket,
65
- path: typing.Annotated[str, Provide(context_adapter)],
66
- ) -> None:
67
- assert path == "/ws"
68
-
69
- await websocket.accept()
70
- await websocket.send_text("test")
71
- await websocket.close()
72
-
73
- with client.websocket_connect("/ws") as websocket:
74
- data = websocket.receive_text()
75
- assert data == "test"