modern-di-litestar 1.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_litestar-1.1.0/.gitignore +22 -0
- modern_di_litestar-1.1.0/PKG-INFO +27 -0
- modern_di_litestar-1.1.0/README.md +6 -0
- modern_di_litestar-1.1.0/modern_di_litestar/__init__.py +8 -0
- modern_di_litestar-1.1.0/modern_di_litestar/main.py +71 -0
- modern_di_litestar-1.1.0/modern_di_litestar/py.typed +0 -0
- modern_di_litestar-1.1.0/pyproject.toml +48 -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,27 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: modern-di-litestar
|
|
3
|
+
Version: 1.1.0
|
|
4
|
+
Summary: Modern-DI integration for LiteStar
|
|
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,LiteStar,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: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
16
|
+
Classifier: Typing :: Typed
|
|
17
|
+
Requires-Python: <4,>=3.10
|
|
18
|
+
Requires-Dist: litestar
|
|
19
|
+
Requires-Dist: modern-di>=1.0.0alpha1
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
"Modern-DI-LiteStar"
|
|
23
|
+
==
|
|
24
|
+
|
|
25
|
+
Integration of [Modern-DI](https://github.com/modern-python/modern-di) to LiteStar
|
|
26
|
+
|
|
27
|
+
📚 [Documentation](https://modern-di.readthedocs.io)
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import contextlib
|
|
2
|
+
import dataclasses
|
|
3
|
+
import typing
|
|
4
|
+
|
|
5
|
+
import litestar
|
|
6
|
+
from litestar.config.app import AppConfig
|
|
7
|
+
from litestar.di import Provide
|
|
8
|
+
from litestar.params import Dependency
|
|
9
|
+
from litestar.plugins import InitPlugin
|
|
10
|
+
from modern_di import AsyncContainer, providers
|
|
11
|
+
from modern_di import Scope as DIScope
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
T_co = typing.TypeVar("T_co", covariant=True)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def fetch_di_container(app_: litestar.Litestar) -> AsyncContainer:
|
|
18
|
+
return typing.cast(AsyncContainer, app_.state.di_container)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@contextlib.asynccontextmanager
|
|
22
|
+
async def _lifespan_manager(app_: litestar.Litestar) -> typing.AsyncIterator[None]:
|
|
23
|
+
container = fetch_di_container(app_)
|
|
24
|
+
async with container:
|
|
25
|
+
yield
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class ModernDIPlugin(InitPlugin):
|
|
29
|
+
__slots__ = ("container",)
|
|
30
|
+
|
|
31
|
+
def __init__(self, container: AsyncContainer) -> None:
|
|
32
|
+
self.container = container
|
|
33
|
+
|
|
34
|
+
def on_app_init(self, app_config: AppConfig) -> AppConfig:
|
|
35
|
+
app_config.state.di_container = self.container
|
|
36
|
+
app_config.dependencies["di_container"] = Provide(build_di_container)
|
|
37
|
+
app_config.lifespan.append(_lifespan_manager)
|
|
38
|
+
return app_config
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
async def build_di_container(
|
|
42
|
+
request: litestar.Request[typing.Any, typing.Any, typing.Any],
|
|
43
|
+
) -> typing.AsyncIterator[AsyncContainer]:
|
|
44
|
+
context: dict[type[typing.Any], typing.Any] = {}
|
|
45
|
+
scope: DIScope | None
|
|
46
|
+
if isinstance(request, litestar.WebSocket):
|
|
47
|
+
context[litestar.WebSocket] = request
|
|
48
|
+
scope = DIScope.SESSION
|
|
49
|
+
else:
|
|
50
|
+
context[litestar.Request] = request
|
|
51
|
+
scope = DIScope.REQUEST
|
|
52
|
+
container: AsyncContainer = fetch_di_container(request.app)
|
|
53
|
+
async with container.build_child_container(context=context, scope=scope) as request_container:
|
|
54
|
+
yield request_container
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@dataclasses.dataclass(slots=True, frozen=True)
|
|
58
|
+
class _Dependency(typing.Generic[T_co]):
|
|
59
|
+
dependency: providers.AbstractProvider[T_co] | type[T_co]
|
|
60
|
+
|
|
61
|
+
async def __call__(
|
|
62
|
+
self, di_container: typing.Annotated[AsyncContainer | None, Dependency(skip_validation=True)] = None
|
|
63
|
+
) -> T_co | None:
|
|
64
|
+
assert di_container
|
|
65
|
+
if isinstance(self.dependency, providers.AbstractProvider):
|
|
66
|
+
return await di_container.resolve_provider(self.dependency)
|
|
67
|
+
return await di_container.resolve(dependency_type=self.dependency)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def FromDI(dependency: providers.AbstractProvider[T_co] | type[T_co]) -> Provide: # noqa: N802
|
|
71
|
+
return Provide(dependency=_Dependency(dependency), use_cache=False)
|
|
File without changes
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "modern-di-litestar"
|
|
3
|
+
description = "Modern-DI integration for LiteStar"
|
|
4
|
+
authors = [{ name = "Artur Shiriev", email = "me@shiriev.ru" }]
|
|
5
|
+
requires-python = ">=3.10,<4"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
keywords = ["DI", "dependency injector", "ioc-container", "LiteStar", "python"]
|
|
9
|
+
classifiers = [
|
|
10
|
+
"Programming Language :: Python :: 3.10",
|
|
11
|
+
"Programming Language :: Python :: 3.11",
|
|
12
|
+
"Programming Language :: Python :: 3.12",
|
|
13
|
+
"Programming Language :: Python :: 3.13",
|
|
14
|
+
"Programming Language :: Python :: 3.14",
|
|
15
|
+
"Typing :: Typed",
|
|
16
|
+
"Topic :: Software Development :: Libraries",
|
|
17
|
+
]
|
|
18
|
+
dependencies = ["litestar", "modern-di>=1.0.0alpha1"]
|
|
19
|
+
version = "1.1.0"
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
repository = "https://github.com/modern-python/modern-di"
|
|
23
|
+
docs = "https://modern-di.readthedocs.io"
|
|
24
|
+
|
|
25
|
+
[dependency-groups]
|
|
26
|
+
dev = [
|
|
27
|
+
"pytest",
|
|
28
|
+
"pytest-cov",
|
|
29
|
+
"pytest-asyncio",
|
|
30
|
+
"ruff",
|
|
31
|
+
"mypy",
|
|
32
|
+
"typing-extensions",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[build-system]
|
|
36
|
+
requires = ["hatchling"]
|
|
37
|
+
build-backend = "hatchling.build"
|
|
38
|
+
|
|
39
|
+
[tool.hatch.build]
|
|
40
|
+
include = ["modern_di_litestar"]
|
|
41
|
+
|
|
42
|
+
[tool.pytest.ini_options]
|
|
43
|
+
addopts = "--cov=. --cov-report term-missing"
|
|
44
|
+
asyncio_mode = "auto"
|
|
45
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
46
|
+
|
|
47
|
+
[tool.coverage.report]
|
|
48
|
+
exclude_also = ["if typing.TYPE_CHECKING:"]
|