modern-di 0.2.0__tar.gz → 0.2.1__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.2.0/.github/workflows/ci.yml → modern_di-0.2.1/.github/workflows/core-ci.yml +13 -12
- modern_di-0.2.1/.github/workflows/fastapi-ci.yml +54 -0
- modern_di-0.2.1/.github/workflows/publish.yml +37 -0
- modern_di-0.2.1/Justfile +23 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/PKG-INFO +2 -3
- {modern_di-0.2.0 → modern_di-0.2.1}/README.md +1 -2
- modern_di-0.2.1/integrations/fastapi/README.md +6 -0
- modern_di-0.2.1/integrations/fastapi/modern_di_fastapi/__init__.py +9 -0
- modern_di-0.2.1/integrations/fastapi/modern_di_fastapi/depends.py +24 -0
- modern_di-0.2.1/integrations/fastapi/modern_di_fastapi/middleware.py +27 -0
- modern_di-0.2.1/integrations/fastapi/pyproject.toml +90 -0
- modern_di-0.2.1/integrations/fastapi/tests/test_fastapi_di.py +69 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/modern_di/container.py +12 -4
- {modern_di-0.2.0 → modern_di-0.2.1}/modern_di/resolvers/__init__.py +2 -0
- modern_di-0.2.1/modern_di/resolvers/context_adapter.py +31 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/pyproject.toml +5 -0
- modern_di-0.2.1/tests/resolvers/__init__.py +0 -0
- modern_di-0.2.1/tests/resolvers/test_context_adapter.py +40 -0
- modern_di-0.2.0/.github/workflows/publish.yml +0 -20
- {modern_di-0.2.0 → modern_di-0.2.1}/.gitignore +0 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/.readthedocs.yaml +0 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/LICENSE +0 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/docs/conf.py +0 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/docs/dev/contributing.md +0 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/docs/dev/development-notes.md +0 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/docs/index.md +0 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/docs/requirements.txt +0 -0
- {modern_di-0.2.0 → modern_di-0.2.1/integrations/fastapi}/Justfile +0 -0
- {modern_di-0.2.0 → modern_di-0.2.1/integrations/fastapi}/tests/__init__.py +0 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/modern_di/__init__.py +0 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/modern_di/graph.py +0 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/modern_di/py.typed +0 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/modern_di/resolver_state.py +0 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/modern_di/resolvers/base.py +0 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/modern_di/resolvers/factory.py +0 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/modern_di/resolvers/resource.py +0 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/modern_di/resolvers/singleton.py +0 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/modern_di/scope.py +0 -0
- {modern_di-0.2.0/tests/resolvers → modern_di-0.2.1/tests}/__init__.py +0 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/tests/creators.py +0 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/tests/resolvers/test_factory.py +0 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/tests/resolvers/test_resource.py +0 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/tests/resolvers/test_singleton.py +0 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/tests/test_container.py +0 -0
- {modern_di-0.2.0 → modern_di-0.2.1}/tests/test_graph.py +0 -0
|
@@ -1,13 +1,21 @@
|
|
|
1
|
-
name:
|
|
1
|
+
name: core ci
|
|
2
2
|
|
|
3
3
|
on:
|
|
4
4
|
push:
|
|
5
5
|
branches:
|
|
6
6
|
- main
|
|
7
|
-
|
|
7
|
+
paths:
|
|
8
|
+
- 'modern_di/**'
|
|
9
|
+
- 'tests/**'
|
|
10
|
+
- '.github/workflows/core-ci.yml'
|
|
11
|
+
pull_request:
|
|
12
|
+
paths:
|
|
13
|
+
- 'modern_di/**'
|
|
14
|
+
- 'tests/**'
|
|
15
|
+
- '.github/workflows/core-ci.yml'
|
|
8
16
|
|
|
9
17
|
concurrency:
|
|
10
|
-
group: ${{ github.head_ref || github.run_id }}
|
|
18
|
+
group: ${{ github.head_ref || github.run_id }} core
|
|
11
19
|
cancel-in-progress: true
|
|
12
20
|
|
|
13
21
|
jobs:
|
|
@@ -19,7 +27,7 @@ jobs:
|
|
|
19
27
|
- uses: astral-sh/setup-uv@v3
|
|
20
28
|
with:
|
|
21
29
|
enable-cache: true
|
|
22
|
-
cache-dependency-glob: "
|
|
30
|
+
cache-dependency-glob: "pyproject.toml"
|
|
23
31
|
- run: uv python install 3.10
|
|
24
32
|
- run: just install lint-ci
|
|
25
33
|
|
|
@@ -39,13 +47,6 @@ jobs:
|
|
|
39
47
|
- uses: astral-sh/setup-uv@v3
|
|
40
48
|
with:
|
|
41
49
|
enable-cache: true
|
|
42
|
-
cache-dependency-glob: "
|
|
50
|
+
cache-dependency-glob: "pyproject.toml"
|
|
43
51
|
- run: uv python install ${{ matrix.python-version }}
|
|
44
52
|
- run: just install test . --cov=. --cov-report xml
|
|
45
|
-
- uses: codecov/codecov-action@v4.0.1
|
|
46
|
-
env:
|
|
47
|
-
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
|
|
48
|
-
with:
|
|
49
|
-
files: ./coverage.xml
|
|
50
|
-
flags: unittests
|
|
51
|
-
name: codecov-${{ matrix.python-version }}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
name: fastapi ci
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
paths:
|
|
8
|
+
- 'integrations/fastapi/**'
|
|
9
|
+
- '.github/workflows/fastapi-ci.yml'
|
|
10
|
+
pull_request:
|
|
11
|
+
paths:
|
|
12
|
+
- 'integrations/fastapi/**'
|
|
13
|
+
- '.github/workflows/fastapi-ci.yml'
|
|
14
|
+
|
|
15
|
+
defaults:
|
|
16
|
+
run:
|
|
17
|
+
working-directory: integrations/fastapi
|
|
18
|
+
|
|
19
|
+
concurrency:
|
|
20
|
+
group: ${{ github.head_ref || github.run_id }} fastapi
|
|
21
|
+
cancel-in-progress: false
|
|
22
|
+
|
|
23
|
+
jobs:
|
|
24
|
+
lint:
|
|
25
|
+
runs-on: ubuntu-latest
|
|
26
|
+
steps:
|
|
27
|
+
- uses: actions/checkout@v4
|
|
28
|
+
- uses: extractions/setup-just@v2
|
|
29
|
+
- uses: astral-sh/setup-uv@v3
|
|
30
|
+
with:
|
|
31
|
+
enable-cache: true
|
|
32
|
+
cache-dependency-glob: "pyproject.toml"
|
|
33
|
+
- run: uv python install 3.10
|
|
34
|
+
- run: just install lint-ci
|
|
35
|
+
|
|
36
|
+
pytest:
|
|
37
|
+
runs-on: ubuntu-latest
|
|
38
|
+
strategy:
|
|
39
|
+
fail-fast: false
|
|
40
|
+
matrix:
|
|
41
|
+
python-version:
|
|
42
|
+
- "3.10"
|
|
43
|
+
- "3.11"
|
|
44
|
+
- "3.12"
|
|
45
|
+
- "3.13"
|
|
46
|
+
steps:
|
|
47
|
+
- uses: actions/checkout@v4
|
|
48
|
+
- uses: extractions/setup-just@v2
|
|
49
|
+
- uses: astral-sh/setup-uv@v3
|
|
50
|
+
with:
|
|
51
|
+
enable-cache: true
|
|
52
|
+
cache-dependency-glob: "pyproject.toml"
|
|
53
|
+
- run: uv python install ${{ matrix.python-version }}
|
|
54
|
+
- run: just install test . --cov=. --cov-report xml
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
name: Publish Package
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types:
|
|
6
|
+
- published
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
publish-core:
|
|
10
|
+
if: startsWith(github.ref_name, 'core')
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
steps:
|
|
13
|
+
- uses: actions/checkout@v4
|
|
14
|
+
- uses: extractions/setup-just@v2
|
|
15
|
+
- uses: astral-sh/setup-uv@v3
|
|
16
|
+
with:
|
|
17
|
+
enable-cache: true
|
|
18
|
+
cache-dependency-glob: "pyproject.toml"
|
|
19
|
+
- run: just publish
|
|
20
|
+
env:
|
|
21
|
+
PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
|
|
22
|
+
|
|
23
|
+
publish-fastapi:
|
|
24
|
+
if: startsWith(github.ref_name, 'fastapi')
|
|
25
|
+
runs-on: ubuntu-latest
|
|
26
|
+
steps:
|
|
27
|
+
- uses: actions/checkout@v4
|
|
28
|
+
- uses: extractions/setup-just@v2
|
|
29
|
+
- uses: astral-sh/setup-uv@v3
|
|
30
|
+
with:
|
|
31
|
+
enable-cache: true
|
|
32
|
+
cache-dependency-glob: "pyproject.toml"
|
|
33
|
+
- run: |
|
|
34
|
+
cd integrations/fastapi
|
|
35
|
+
just publish
|
|
36
|
+
env:
|
|
37
|
+
PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
|
modern_di-0.2.1/Justfile
ADDED
|
@@ -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 tests {{ args }}
|
|
19
|
+
|
|
20
|
+
publish:
|
|
21
|
+
rm -rf dist
|
|
22
|
+
uv build
|
|
23
|
+
uv publish --token $PYPI_TOKEN
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: modern-di
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.1
|
|
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
|
|
@@ -19,7 +19,6 @@ Description-Content-Type: text/markdown
|
|
|
19
19
|
|
|
20
20
|
"Modern-DI"
|
|
21
21
|
==
|
|
22
|
-
[](https://codecov.io/gh/modern-python/modern-di)
|
|
23
22
|
[](https://mypy.readthedocs.io/en/stable/getting_started.html#strict-mode-and-configuration)
|
|
24
23
|
[](https://pypi.python.org/pypi/modern-di)
|
|
25
24
|
[](https://pypistats.org/packages/modern-di)
|
|
@@ -27,7 +26,7 @@ Description-Content-Type: text/markdown
|
|
|
27
26
|
|
|
28
27
|
Dependency injection framework for Python inspired by `dependency-injector` and `dishka`.
|
|
29
28
|
|
|
30
|
-
It is
|
|
29
|
+
It is in early development state and gives you the following:
|
|
31
30
|
- DI framework with IOC-container and scopes.
|
|
32
31
|
- Async and sync resolving.
|
|
33
32
|
- Python 3.10-3.13 support.
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
"Modern-DI"
|
|
2
2
|
==
|
|
3
|
-
[](https://codecov.io/gh/modern-python/modern-di)
|
|
4
3
|
[](https://mypy.readthedocs.io/en/stable/getting_started.html#strict-mode-and-configuration)
|
|
5
4
|
[](https://pypi.python.org/pypi/modern-di)
|
|
6
5
|
[](https://pypistats.org/packages/modern-di)
|
|
@@ -8,7 +7,7 @@
|
|
|
8
7
|
|
|
9
8
|
Dependency injection framework for Python inspired by `dependency-injector` and `dishka`.
|
|
10
9
|
|
|
11
|
-
It is
|
|
10
|
+
It is in early development state and gives you the following:
|
|
12
11
|
- DI framework with IOC-container and scopes.
|
|
13
12
|
- Async and sync resolving.
|
|
14
13
|
- Python 3.10-3.13 support.
|
|
@@ -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:"]
|
|
@@ -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"
|
|
@@ -14,15 +14,22 @@ T_co = typing.TypeVar("T_co", covariant=True)
|
|
|
14
14
|
|
|
15
15
|
|
|
16
16
|
class Container(contextlib.AbstractAsyncContextManager["Container"]):
|
|
17
|
-
__slots__ = "scope", "parent_container", "_is_async", "_resolver_states", "_overrides"
|
|
17
|
+
__slots__ = "scope", "parent_container", "context", "_is_async", "_resolver_states", "_overrides"
|
|
18
18
|
|
|
19
|
-
def __init__(
|
|
19
|
+
def __init__(
|
|
20
|
+
self,
|
|
21
|
+
*,
|
|
22
|
+
scope: enum.IntEnum,
|
|
23
|
+
parent_container: typing.Optional["Container"] = None,
|
|
24
|
+
context: dict[str, typing.Any] | None = None,
|
|
25
|
+
) -> None:
|
|
20
26
|
if scope.value != 1 and parent_container is None:
|
|
21
27
|
msg = "Only first scope can be used without parent_container"
|
|
22
28
|
raise RuntimeError(msg)
|
|
23
29
|
|
|
24
30
|
self.scope = scope
|
|
25
31
|
self.parent_container = parent_container
|
|
32
|
+
self.context: dict[str, typing.Any] = context or {}
|
|
26
33
|
self._is_async: bool | None = None
|
|
27
34
|
self._resolver_states: dict[str, ResolverState[typing.Any]] = {}
|
|
28
35
|
self._overrides: dict[str, typing.Any] = {}
|
|
@@ -30,13 +37,14 @@ class Container(contextlib.AbstractAsyncContextManager["Container"]):
|
|
|
30
37
|
def _exit(self) -> None:
|
|
31
38
|
self._is_async = None
|
|
32
39
|
self._resolver_states = {}
|
|
40
|
+
self._overrides = {}
|
|
33
41
|
|
|
34
42
|
def _check_entered(self) -> None:
|
|
35
43
|
if self._is_async is None:
|
|
36
44
|
msg = "Enter the context first"
|
|
37
45
|
raise RuntimeError(msg)
|
|
38
46
|
|
|
39
|
-
def build_child_container(self) -> "typing_extensions.Self":
|
|
47
|
+
def build_child_container(self, context: dict[str, typing.Any] | None = None) -> "typing_extensions.Self":
|
|
40
48
|
self._check_entered()
|
|
41
49
|
|
|
42
50
|
try:
|
|
@@ -45,7 +53,7 @@ class Container(contextlib.AbstractAsyncContextManager["Container"]):
|
|
|
45
53
|
msg = f"Max scope is reached, {self.scope.name}"
|
|
46
54
|
raise RuntimeError(msg) from exc
|
|
47
55
|
|
|
48
|
-
return self.__class__(scope=new_scope, parent_container=self)
|
|
56
|
+
return self.__class__(scope=new_scope, parent_container=self, context=context)
|
|
49
57
|
|
|
50
58
|
def find_container(self, scope: enum.IntEnum) -> "typing_extensions.Self":
|
|
51
59
|
container = self
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
from modern_di.resolvers.base import AbstractResolver, BaseCreatorResolver
|
|
2
|
+
from modern_di.resolvers.context_adapter import ContextAdapter
|
|
2
3
|
from modern_di.resolvers.factory import Factory
|
|
3
4
|
from modern_di.resolvers.resource import Resource
|
|
4
5
|
from modern_di.resolvers.singleton import Singleton
|
|
@@ -7,6 +8,7 @@ from modern_di.resolvers.singleton import Singleton
|
|
|
7
8
|
__all__ = [
|
|
8
9
|
"AbstractResolver",
|
|
9
10
|
"BaseCreatorResolver",
|
|
11
|
+
"ContextAdapter",
|
|
10
12
|
"Factory",
|
|
11
13
|
"Singleton",
|
|
12
14
|
"Resource",
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import enum
|
|
2
|
+
import typing
|
|
3
|
+
|
|
4
|
+
from modern_di import Container
|
|
5
|
+
from modern_di.resolvers import AbstractResolver
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
T_co = typing.TypeVar("T_co", covariant=True)
|
|
9
|
+
P = typing.ParamSpec("P")
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ContextAdapter(AbstractResolver[T_co]):
|
|
13
|
+
__slots__ = [*AbstractResolver.BASE_SLOTS, "_function"]
|
|
14
|
+
|
|
15
|
+
def __init__(
|
|
16
|
+
self,
|
|
17
|
+
scope: enum.IntEnum,
|
|
18
|
+
function: typing.Callable[..., T_co],
|
|
19
|
+
) -> None:
|
|
20
|
+
super().__init__(scope)
|
|
21
|
+
self._function = function
|
|
22
|
+
|
|
23
|
+
async def async_resolve(self, container: Container) -> T_co:
|
|
24
|
+
return self.sync_resolve(container)
|
|
25
|
+
|
|
26
|
+
def sync_resolve(self, container: Container) -> T_co:
|
|
27
|
+
container = container.find_container(self.scope)
|
|
28
|
+
if (override := container.fetch_override(self.resolver_id)) is not None:
|
|
29
|
+
return typing.cast(T_co, override)
|
|
30
|
+
|
|
31
|
+
return self._function(**container.context)
|
|
@@ -41,10 +41,14 @@ build-backend = "hatchling.build"
|
|
|
41
41
|
|
|
42
42
|
[tool.hatch.version]
|
|
43
43
|
source = "vcs"
|
|
44
|
+
fallback-version = "0"
|
|
44
45
|
|
|
45
46
|
[tool.mypy]
|
|
46
47
|
python_version = "3.10"
|
|
47
48
|
strict = true
|
|
49
|
+
exclude = [
|
|
50
|
+
"integrations",
|
|
51
|
+
]
|
|
48
52
|
|
|
49
53
|
[tool.ruff]
|
|
50
54
|
fix = true
|
|
@@ -53,6 +57,7 @@ line-length = 120
|
|
|
53
57
|
target-version = "py310"
|
|
54
58
|
extend-exclude = [
|
|
55
59
|
"docs",
|
|
60
|
+
"integrations"
|
|
56
61
|
]
|
|
57
62
|
|
|
58
63
|
[tool.ruff.lint]
|
|
File without changes
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import datetime
|
|
2
|
+
|
|
3
|
+
from modern_di import Container, Scope, resolvers
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def context_adapter_function(*, now: datetime.datetime, **_: object) -> datetime.datetime:
|
|
7
|
+
return now
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
context_adapter = resolvers.ContextAdapter(Scope.APP, context_adapter_function)
|
|
11
|
+
request_context_adapter = resolvers.ContextAdapter(Scope.REQUEST, context_adapter_function)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
async def test_context_adapter() -> None:
|
|
15
|
+
now = datetime.datetime.now(tz=datetime.timezone.utc)
|
|
16
|
+
async with Container(scope=Scope.APP, context={"now": now}) as app_container:
|
|
17
|
+
instance1 = await context_adapter.async_resolve(app_container)
|
|
18
|
+
instance2 = context_adapter.sync_resolve(app_container)
|
|
19
|
+
assert instance1 is instance2 is now
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
async def test_context_adapter_in_request_scope() -> None:
|
|
23
|
+
now = datetime.datetime.now(tz=datetime.timezone.utc)
|
|
24
|
+
async with (
|
|
25
|
+
Container(scope=Scope.APP) as app_container,
|
|
26
|
+
app_container.build_child_container(context={"now": now}) as request_container,
|
|
27
|
+
):
|
|
28
|
+
instance1 = await request_context_adapter.async_resolve(request_container)
|
|
29
|
+
instance2 = request_context_adapter.sync_resolve(request_container)
|
|
30
|
+
assert instance1 is instance2 is now
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
async def test_context_adapter_override() -> None:
|
|
34
|
+
now = datetime.datetime.now(tz=datetime.timezone.utc)
|
|
35
|
+
async with Container(scope=Scope.APP, context={"now": now}) as app_container:
|
|
36
|
+
instance1 = await context_adapter.async_resolve(app_container)
|
|
37
|
+
context_adapter.override(datetime.datetime.now(tz=datetime.timezone.utc), container=app_container)
|
|
38
|
+
instance2 = context_adapter.sync_resolve(app_container)
|
|
39
|
+
assert instance1 is now
|
|
40
|
+
assert instance2 is not now
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
name: Publish Package
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
release:
|
|
5
|
-
types:
|
|
6
|
-
- published
|
|
7
|
-
|
|
8
|
-
jobs:
|
|
9
|
-
publish:
|
|
10
|
-
runs-on: ubuntu-latest
|
|
11
|
-
steps:
|
|
12
|
-
- uses: actions/checkout@v4
|
|
13
|
-
- uses: extractions/setup-just@v2
|
|
14
|
-
- uses: astral-sh/setup-uv@v3
|
|
15
|
-
with:
|
|
16
|
-
enable-cache: true
|
|
17
|
-
cache-dependency-glob: "**/pyproject.toml"
|
|
18
|
-
- run: just publish
|
|
19
|
-
env:
|
|
20
|
-
PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
|
|
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
|