modern-di 0.4.1__tar.gz → 0.4.3__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.

Potentially problematic release.


This version of modern-di might be problematic. Click here for more details.

Files changed (56) hide show
  1. {modern_di-0.4.1 → modern_di-0.4.3}/.github/workflows/publish.yml +7 -18
  2. {modern_di-0.4.1 → modern_di-0.4.3}/Justfile +2 -2
  3. {modern_di-0.4.1 → modern_di-0.4.3}/PKG-INFO +1 -1
  4. {modern_di-0.4.1 → modern_di-0.4.3}/integrations/fastapi/Justfile +0 -5
  5. modern_di-0.4.3/integrations/fastapi/modern_di_fastapi/__init__.py +9 -0
  6. modern_di-0.4.3/integrations/fastapi/modern_di_fastapi/main.py +38 -0
  7. modern_di-0.4.3/integrations/fastapi/tests/conftest.py +28 -0
  8. modern_di-0.4.3/integrations/fastapi/tests/dependencies.py +17 -0
  9. modern_di-0.4.3/integrations/fastapi/tests/test_fastapi_di.py +30 -0
  10. {modern_di-0.4.1 → modern_di-0.4.3}/modern_di/container.py +5 -0
  11. {modern_di-0.4.1 → modern_di-0.4.3}/tests/providers/test_factory.py +6 -0
  12. modern_di-0.4.1/integrations/fastapi/modern_di_fastapi/__init__.py +0 -9
  13. modern_di-0.4.1/integrations/fastapi/modern_di_fastapi/depends.py +0 -24
  14. modern_di-0.4.1/integrations/fastapi/modern_di_fastapi/middleware.py +0 -27
  15. modern_di-0.4.1/integrations/fastapi/tests/test_fastapi_di.py +0 -69
  16. {modern_di-0.4.1 → modern_di-0.4.3}/.github/workflows/core-ci.yml +0 -0
  17. {modern_di-0.4.1 → modern_di-0.4.3}/.github/workflows/fastapi-ci.yml +0 -0
  18. {modern_di-0.4.1 → modern_di-0.4.3}/.gitignore +0 -0
  19. {modern_di-0.4.1 → modern_di-0.4.3}/.readthedocs.yaml +0 -0
  20. {modern_di-0.4.1 → modern_di-0.4.3}/LICENSE +0 -0
  21. {modern_di-0.4.1 → modern_di-0.4.3}/README.md +0 -0
  22. {modern_di-0.4.1 → modern_di-0.4.3}/docs/conf.py +0 -0
  23. {modern_di-0.4.1 → modern_di-0.4.3}/docs/dev/contributing.md +0 -0
  24. {modern_di-0.4.1 → modern_di-0.4.3}/docs/dev/development-notes.md +0 -0
  25. {modern_di-0.4.1 → modern_di-0.4.3}/docs/index.md +0 -0
  26. {modern_di-0.4.1 → modern_di-0.4.3}/docs/requirements.txt +0 -0
  27. {modern_di-0.4.1 → modern_di-0.4.3}/integrations/fastapi/README.md +0 -0
  28. {modern_di-0.4.1 → modern_di-0.4.3}/integrations/fastapi/modern_di_fastapi/py.typed +0 -0
  29. {modern_di-0.4.1 → modern_di-0.4.3}/integrations/fastapi/pyproject.toml +0 -0
  30. {modern_di-0.4.1 → modern_di-0.4.3}/integrations/fastapi/tests/__init__.py +0 -0
  31. {modern_di-0.4.1 → modern_di-0.4.3}/modern_di/__init__.py +0 -0
  32. {modern_di-0.4.1 → modern_di-0.4.3}/modern_di/graph.py +0 -0
  33. {modern_di-0.4.1 → modern_di-0.4.3}/modern_di/provider_state.py +0 -0
  34. {modern_di-0.4.1 → modern_di-0.4.3}/modern_di/providers/__init__.py +0 -0
  35. {modern_di-0.4.1 → modern_di-0.4.3}/modern_di/providers/abstract.py +0 -0
  36. {modern_di-0.4.1 → modern_di-0.4.3}/modern_di/providers/context_adapter.py +0 -0
  37. {modern_di-0.4.1 → modern_di-0.4.3}/modern_di/providers/dict.py +0 -0
  38. {modern_di-0.4.1 → modern_di-0.4.3}/modern_di/providers/factory.py +0 -0
  39. {modern_di-0.4.1 → modern_di-0.4.3}/modern_di/providers/list.py +0 -0
  40. {modern_di-0.4.1 → modern_di-0.4.3}/modern_di/providers/resource.py +0 -0
  41. {modern_di-0.4.1 → modern_di-0.4.3}/modern_di/providers/selector.py +0 -0
  42. {modern_di-0.4.1 → modern_di-0.4.3}/modern_di/providers/singleton.py +0 -0
  43. {modern_di-0.4.1 → modern_di-0.4.3}/modern_di/py.typed +0 -0
  44. {modern_di-0.4.1 → modern_di-0.4.3}/modern_di/scope.py +0 -0
  45. {modern_di-0.4.1 → modern_di-0.4.3}/pyproject.toml +0 -0
  46. {modern_di-0.4.1 → modern_di-0.4.3}/tests/__init__.py +0 -0
  47. {modern_di-0.4.1 → modern_di-0.4.3}/tests/creators.py +0 -0
  48. {modern_di-0.4.1 → modern_di-0.4.3}/tests/providers/__init__.py +0 -0
  49. {modern_di-0.4.1 → modern_di-0.4.3}/tests/providers/test_context_adapter.py +0 -0
  50. {modern_di-0.4.1 → modern_di-0.4.3}/tests/providers/test_dict.py +0 -0
  51. {modern_di-0.4.1 → modern_di-0.4.3}/tests/providers/test_list.py +0 -0
  52. {modern_di-0.4.1 → modern_di-0.4.3}/tests/providers/test_resource.py +0 -0
  53. {modern_di-0.4.1 → modern_di-0.4.3}/tests/providers/test_selector.py +0 -0
  54. {modern_di-0.4.1 → modern_di-0.4.3}/tests/providers/test_singleton.py +0 -0
  55. {modern_di-0.4.1 → modern_di-0.4.3}/tests/test_container.py +0 -0
  56. {modern_di-0.4.1 → modern_di-0.4.3}/tests/test_graph.py +0 -0
@@ -6,8 +6,7 @@ on:
6
6
  - published
7
7
 
8
8
  jobs:
9
- publish-core:
10
- if: startsWith(github.ref_name, 'core')
9
+ publish:
11
10
  runs-on: ubuntu-latest
12
11
  steps:
13
12
  - uses: actions/checkout@v4
@@ -16,26 +15,16 @@ jobs:
16
15
  with:
17
16
  enable-cache: true
18
17
  cache-dependency-glob: "pyproject.toml"
19
- - run: just publish
18
+
19
+ - if: startsWith(github.ref_name, 'core')
20
+ run: just publish modern-di
20
21
  env:
21
22
  PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
22
23
 
23
- publish-fastapi:
24
- if: startsWith(github.ref_name, 'fastapi')
25
- runs-on: ubuntu-latest
26
- defaults:
27
- run:
28
- working-directory: integrations/fastapi
29
- steps:
30
- - uses: actions/checkout@v4
31
- - uses: extractions/setup-just@v2
32
- - uses: astral-sh/setup-uv@v3
33
- with:
34
- enable-cache: true
35
- cache-dependency-glob: "pyproject.toml"
36
- - run: |
24
+ - if: startsWith(github.ref_name, 'fastapi')
25
+ run: |
37
26
  export SETUPTOOLS_SCM_PRETEND_VERSION=$(echo "${{ github.ref_name }}" | sed 's/^fastapi-//; s/-.*$//')
38
27
  echo "SETUPTOOLS_SCM_PRETEND_VERSION=$SETUPTOOLS_SCM_PRETEND_VERSION"
39
- just publish
28
+ just publish modern-di-fastapi
40
29
  env:
41
30
  PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
@@ -17,7 +17,7 @@ lint-ci:
17
17
  test *args:
18
18
  uv run pytest tests {{ args }}
19
19
 
20
- publish:
20
+ publish package:
21
21
  rm -rf dist
22
- uv build
22
+ uv build --package {{package}}
23
23
  uv publish --token $PYPI_TOKEN
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: modern-di
3
- Version: 0.4.1
3
+ Version: 0.4.3
4
4
  Summary: Dependency Injection framework with IOC-container and scopes
5
5
  Project-URL: repository, https://github.com/modern-python/modern-di
6
6
  Project-URL: docs, https://modern-di.readthedocs.io
@@ -16,8 +16,3 @@ lint-ci:
16
16
 
17
17
  test *args:
18
18
  uv run pytest {{ args }}
19
-
20
- publish:
21
- rm -rf dist
22
- uv build
23
- uv publish --token $PYPI_TOKEN
@@ -0,0 +1,9 @@
1
+ from modern_di_fastapi.main import Provide, build_request_container, fetch_di_container, setup_di
2
+
3
+
4
+ __all__ = [
5
+ "Provide",
6
+ "build_request_container",
7
+ "fetch_di_container",
8
+ "setup_di",
9
+ ]
@@ -0,0 +1,38 @@
1
+ import dataclasses
2
+ import enum
3
+ import typing
4
+
5
+ import fastapi
6
+ from modern_di import Container, Scope, providers
7
+
8
+
9
+ T_co = typing.TypeVar("T_co", covariant=True)
10
+
11
+
12
+ def setup_di(app: fastapi.FastAPI, scope: enum.IntEnum = Scope.APP) -> Container:
13
+ app.state.di_container = Container(scope=scope)
14
+ return app.state.di_container
15
+
16
+
17
+ def fetch_di_container(app: fastapi.FastAPI) -> Container:
18
+ return typing.cast(Container, app.state.di_container)
19
+
20
+
21
+ async def build_request_container(request: fastapi.Request) -> typing.AsyncIterator[Container]:
22
+ container: Container = fetch_di_container(request.app)
23
+ async with container.build_child_container(context={"request": request}) as request_container:
24
+ yield request_container
25
+
26
+
27
+ @dataclasses.dataclass(slots=True, frozen=True)
28
+ class Dependency(typing.Generic[T_co]):
29
+ dependency: providers.AbstractProvider[T_co]
30
+
31
+ async def __call__(
32
+ self, request_container: typing.Annotated[Container, fastapi.Depends(build_request_container)]
33
+ ) -> T_co:
34
+ return await self.dependency.async_resolve(request_container)
35
+
36
+
37
+ def Provide(dependency: providers.AbstractProvider[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
38
+ return typing.cast(T_co, fastapi.Depends(dependency=Dependency(dependency), use_cache=use_cache))
@@ -0,0 +1,28 @@
1
+ import contextlib
2
+ import typing
3
+
4
+ import fastapi
5
+ import httpx
6
+ import pytest
7
+ from asgi_lifespan import LifespanManager
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) -> httpx.AsyncClient:
28
+ return httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://test")
@@ -0,0 +1,17 @@
1
+ import dataclasses
2
+
3
+ import fastapi
4
+
5
+
6
+ @dataclasses.dataclass(kw_only=True, slots=True)
7
+ class SimpleCreator:
8
+ dep1: str
9
+
10
+
11
+ @dataclasses.dataclass(kw_only=True, slots=True)
12
+ class DependentCreator:
13
+ dep1: SimpleCreator
14
+
15
+
16
+ def context_adapter_function(*, request: fastapi.Request, **_: object) -> str:
17
+ return request.method
@@ -0,0 +1,30 @@
1
+ import typing
2
+
3
+ import fastapi
4
+ import httpx
5
+ from modern_di import Scope, providers
6
+ from starlette import status
7
+
8
+ from modern_di_fastapi import Provide
9
+ from tests.dependencies import DependentCreator, SimpleCreator, context_adapter_function
10
+
11
+
12
+ app_factory = providers.Factory(Scope.APP, SimpleCreator, dep1="original")
13
+ request_factory = providers.Factory(Scope.REQUEST, DependentCreator, dep1=app_factory.cast)
14
+ context_adapter = providers.ContextAdapter(Scope.REQUEST, context_adapter_function)
15
+
16
+
17
+ async def test_read_main(client: httpx.AsyncClient, app: fastapi.FastAPI) -> None:
18
+ @app.get("/")
19
+ async def read_root(
20
+ app_factory_instance: typing.Annotated[SimpleCreator, Provide(app_factory)],
21
+ request_factory_instance: typing.Annotated[DependentCreator, Provide(request_factory)],
22
+ method: typing.Annotated[str, Provide(context_adapter)],
23
+ ) -> str:
24
+ assert isinstance(app_factory_instance, SimpleCreator)
25
+ assert isinstance(request_factory_instance, DependentCreator)
26
+ return method
27
+
28
+ response = await client.get("/")
29
+ assert response.status_code == status.HTTP_200_OK
30
+ assert response.json() == "GET"
@@ -38,6 +38,7 @@ class Container(contextlib.AbstractAsyncContextManager["Container"]):
38
38
  self._is_async = None
39
39
  self._provider_states = {}
40
40
  self._overrides = {}
41
+ self.context = {}
41
42
 
42
43
  def _check_entered(self) -> None:
43
44
  if self._is_async is None:
@@ -57,6 +58,10 @@ class Container(contextlib.AbstractAsyncContextManager["Container"]):
57
58
 
58
59
  def find_container(self, scope: enum.IntEnum) -> "typing_extensions.Self":
59
60
  container = self
61
+ if container.scope < scope:
62
+ msg = f"Scope {scope.name} is not initialized"
63
+ raise RuntimeError(msg)
64
+
60
65
  while container.scope > scope and container.parent_container:
61
66
  container = typing.cast("typing_extensions.Self", container.parent_container)
62
67
  return container
@@ -85,3 +85,9 @@ async def test_factory_wrong_dependency_scope() -> None:
85
85
  request_factory_ = providers.Factory(Scope.REQUEST, SimpleCreator, dep1="original")
86
86
  with pytest.raises(RuntimeError, match="Scope of dependency cannot be more than scope of dependent"):
87
87
  providers.Singleton(Scope.APP, some_factory, request_factory_.cast)
88
+
89
+
90
+ async def test_factory_scope_is_not_initialized() -> None:
91
+ async with Container(scope=Scope.APP) as app_container:
92
+ with pytest.raises(RuntimeError, match="Scope REQUEST is not initialize"):
93
+ await request_factory.async_resolve(app_container)
@@ -1,9 +0,0 @@
1
- from modern_di_fastapi.depends import FromDI, setup_modern_di
2
- from modern_di_fastapi.middleware import ContainerMiddleware
3
-
4
-
5
- __all__ = [
6
- "ContainerMiddleware",
7
- "FromDI",
8
- "setup_modern_di",
9
- ]
@@ -1,24 +0,0 @@
1
- import dataclasses
2
- import typing
3
-
4
- import fastapi
5
- from modern_di import Container, providers
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: providers.AbstractProvider[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: providers.AbstractProvider[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: Container, app: fastapi.FastAPI) -> None:
24
- app.state.modern_di_container = container
@@ -1,27 +0,0 @@
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)
@@ -1,69 +0,0 @@
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, providers
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 = providers.Factory(Scope.APP, SimpleCreator, dep1="original")
45
- request_factory = providers.Factory(Scope.REQUEST, DependentCreator, dep1=app_factory.cast)
46
- context_adapter = providers.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"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes