modern-di 0.4.2__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.
- {modern_di-0.4.2 → modern_di-0.4.3}/.github/workflows/publish.yml +8 -4
- {modern_di-0.4.2 → modern_di-0.4.3}/PKG-INFO +1 -1
- modern_di-0.4.3/integrations/fastapi/Justfile +18 -0
- modern_di-0.4.3/integrations/fastapi/modern_di_fastapi/__init__.py +9 -0
- modern_di-0.4.3/integrations/fastapi/modern_di_fastapi/main.py +38 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/integrations/fastapi/pyproject.toml +0 -3
- modern_di-0.4.3/integrations/fastapi/tests/conftest.py +28 -0
- modern_di-0.4.3/integrations/fastapi/tests/dependencies.py +17 -0
- modern_di-0.4.3/integrations/fastapi/tests/test_fastapi_di.py +30 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/modern_di/container.py +5 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/tests/providers/test_factory.py +6 -0
- modern_di-0.4.2/integrations/fastapi/modern_di_fastapi/__init__.py +0 -9
- modern_di-0.4.2/integrations/fastapi/modern_di_fastapi/depends.py +0 -24
- modern_di-0.4.2/integrations/fastapi/modern_di_fastapi/middleware.py +0 -27
- modern_di-0.4.2/integrations/fastapi/tests/test_fastapi_di.py +0 -69
- {modern_di-0.4.2 → modern_di-0.4.3}/.github/workflows/core-ci.yml +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/.github/workflows/fastapi-ci.yml +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/.gitignore +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/.readthedocs.yaml +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/Justfile +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/LICENSE +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/README.md +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/docs/conf.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/docs/dev/contributing.md +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/docs/dev/development-notes.md +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/docs/index.md +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/docs/requirements.txt +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/integrations/fastapi/README.md +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/integrations/fastapi/modern_di_fastapi/py.typed +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/integrations/fastapi/tests/__init__.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/modern_di/__init__.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/modern_di/graph.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/modern_di/provider_state.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/modern_di/providers/__init__.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/modern_di/providers/abstract.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/modern_di/providers/context_adapter.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/modern_di/providers/dict.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/modern_di/providers/factory.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/modern_di/providers/list.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/modern_di/providers/resource.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/modern_di/providers/selector.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/modern_di/providers/singleton.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/modern_di/py.typed +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/modern_di/scope.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/pyproject.toml +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/tests/__init__.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/tests/creators.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/tests/providers/__init__.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/tests/providers/test_context_adapter.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/tests/providers/test_dict.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/tests/providers/test_list.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/tests/providers/test_resource.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/tests/providers/test_selector.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/tests/providers/test_singleton.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/tests/test_container.py +0 -0
- {modern_di-0.4.2 → modern_di-0.4.3}/tests/test_graph.py +0 -0
|
@@ -15,12 +15,16 @@ jobs:
|
|
|
15
15
|
with:
|
|
16
16
|
enable-cache: true
|
|
17
17
|
cache-dependency-glob: "pyproject.toml"
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
-
|
|
24
|
-
|
|
24
|
+
- if: startsWith(github.ref_name, 'fastapi')
|
|
25
|
+
run: |
|
|
26
|
+
export SETUPTOOLS_SCM_PRETEND_VERSION=$(echo "${{ github.ref_name }}" | sed 's/^fastapi-//; s/-.*$//')
|
|
27
|
+
echo "SETUPTOOLS_SCM_PRETEND_VERSION=$SETUPTOOLS_SCM_PRETEND_VERSION"
|
|
28
|
+
just publish modern-di-fastapi
|
|
25
29
|
env:
|
|
26
30
|
PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: modern-di
|
|
3
|
-
Version: 0.4.
|
|
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
|
|
@@ -0,0 +1,18 @@
|
|
|
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 }}
|
|
@@ -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,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
|
|
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
|
|
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
|
|
File without changes
|
|
File without changes
|