Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: main

on:
push:
branches:
- main
pull_request: {}

concurrency:
group: ${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: extractions/setup-just@v2
- uses: astral-sh/setup-uv@v3
with:
enable-cache: true
cache-dependency-glob: "**/pyproject.toml"
- run: uv python install 3.10
- run: just install lint-ci

pytest:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version:
- "3.10"
- "3.11"
- "3.12"
- "3.13"
- "3.14"
steps:
- uses: actions/checkout@v4
- uses: extractions/setup-just@v2
- uses: astral-sh/setup-uv@v3
with:
enable-cache: true
cache-dependency-glob: "**/pyproject.toml"
- run: uv python install ${{ matrix.python-version }}
- run: just install
- run: just test . --cov=. --cov-report xml
- uses: codecov/codecov-action@v4.0.1
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
with:
files: ./coverage.xml
flags: unittests
name: codecov-${{ matrix.python-version }}
17 changes: 17 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Publish Package

on:
release:
types:
- published

jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: extractions/setup-just@v2
- uses: astral-sh/setup-uv@v3
- run: just publish
env:
PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generic things
*.pyc
*~
__pycache__/*
*.swp
*.sqlite3
*.map
.vscode
.idea
.DS_Store
.env
.mypy_cache
.pytest_cache
.ruff_cache
.coverage
htmlcov/
coverage.xml
pytest.xml
dist/
.python-version
.venv
uv.lock
29 changes: 29 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
default: install lint test

install:
uv lock --upgrade
uv sync --all-extras --frozen --group lint

lint:
uv run eof-fixer .
uv run ruff format
uv run ruff check --fix
uv run mypy .

lint-ci:
uv run eof-fixer . --check
uv run ruff format --check
uv run ruff check --no-fix
uv run mypy .

test *args:
uv run --no-sync pytest {{ args }}

test-branch:
@just test --cov-branch

publish:
rm -rf dist
uv version $GITHUB_REF_NAME
uv build
uv publish --token $PYPI_TOKEN
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 modern-python

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
18 changes: 18 additions & 0 deletions modern_di_fastapi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from modern_di_fastapi.main import (
FromDI,
build_di_container,
fastapi_request_provider,
fastapi_websocket_provider,
fetch_di_container,
setup_di,
)


__all__ = [
"FromDI",
"build_di_container",
"fastapi_request_provider",
"fastapi_websocket_provider",
"fetch_di_container",
"setup_di",
]
71 changes: 71 additions & 0 deletions modern_di_fastapi/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import contextlib
import dataclasses
import typing

import fastapi
from fastapi.routing import _merge_lifespan_context
from modern_di import Container, Scope, providers
from starlette.requests import HTTPConnection


T_co = typing.TypeVar("T_co", covariant=True)


fastapi_request_provider = providers.ContextProvider(scope=Scope.REQUEST, context_type=fastapi.Request)
fastapi_websocket_provider = providers.ContextProvider(scope=Scope.SESSION, context_type=fastapi.WebSocket)


def fetch_di_container(app_: fastapi.FastAPI) -> Container:
return typing.cast(Container, app_.state.di_container)


@contextlib.asynccontextmanager
async def _lifespan_manager(app_: fastapi.FastAPI) -> typing.AsyncIterator[None]:
container = fetch_di_container(app_)
try:
yield
finally:
await container.close_async()


def setup_di(app: fastapi.FastAPI, container: Container) -> Container:
app.state.di_container = container
container.providers_registry.add_providers(fastapi_request_provider, fastapi_websocket_provider)
old_lifespan_manager = app.router.lifespan_context
app.router.lifespan_context = _merge_lifespan_context(
old_lifespan_manager,
_lifespan_manager,
)
return container


async def build_di_container(connection: HTTPConnection) -> typing.AsyncIterator[Container]:
context: dict[type[typing.Any], typing.Any] = {}
scope: Scope | None = None
if isinstance(connection, fastapi.Request):
scope = Scope.REQUEST
context[fastapi.Request] = connection
elif isinstance(connection, fastapi.WebSocket):
context[fastapi.WebSocket] = connection
scope = Scope.SESSION
container = fetch_di_container(connection.app).build_child_container(context=context, scope=scope)
try:
yield container
finally:
await container.close_async()


@dataclasses.dataclass(slots=True, frozen=True)
class Dependency(typing.Generic[T_co]):
dependency: providers.AbstractProvider[T_co] | type[T_co]

async def __call__(
self, request_container: typing.Annotated[Container, fastapi.Depends(build_di_container)]
) -> T_co:
if isinstance(self.dependency, providers.AbstractProvider):
return request_container.resolve_provider(self.dependency)
return request_container.resolve(dependency_type=self.dependency)


def FromDI(dependency: providers.AbstractProvider[T_co] | type[T_co], *, use_cache: bool = True) -> T_co: # noqa: N802
return typing.cast(T_co, fastapi.Depends(dependency=Dependency(dependency), use_cache=use_cache))
Empty file added modern_di_fastapi/py.typed
Empty file.
83 changes: 83 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
[project]
name = "modern-di-fastapi"
description = "Modern-DI integration for FastAPI"
authors = [{ name = "Artur Shiriev", email = "me@shiriev.ru" }]
requires-python = ">=3.10,<4"
license = "MIT"
readme = "README.md"
keywords = ["DI", "dependency injector", "ioc-container", "FastAPI", "python"]
classifiers = [
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: 3.14",
"Typing :: Typed",
"Topic :: Software Development :: Libraries",
]
dependencies = ["fastapi>=0.100", "modern-di>=2,<3"]
version = "0"

[project.urls]
repository = "https://github.com/modern-python/modern-di-fastapi"
docs = "https://modern-di.readthedocs.io"

[dependency-groups]
dev = [
"pytest",
"pytest-cov",
"pytest-asyncio",
"httpx",
]
lint = [
"mypy",
"ruff",
"eof-fixer",
"typing-extensions",
]

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build]
include = ["modern_di_fastapi"]

[tool.mypy]
python_version = "3.10"
strict = true

[tool.ruff]
fix = false
unsafe-fixes = true
line-length = 120
target-version = "py310"

[tool.ruff.format]
docstring-code-format = true

[tool.ruff.lint]
select = ["ALL"]
ignore = [
"D1", # allow missing docstrings
"S101", # allow asserts
"TCH", # ignore flake8-type-checking
"FBT", # allow boolean args
"D203", # "one-blank-line-before-class" conflicting with D211
"D213", # "multi-line-summary-second-line" conflicting with D212
"COM812", # flake8-commas "Trailing comma missing"
"ISC001", # flake8-implicit-str-concat
"G004", # allow f-strings in logging
]
isort.lines-after-imports = 2
isort.no-lines-before = ["standard-library", "local-folder"]

[tool.pytest.ini_options]
addopts = "--cov=. --cov-report term-missing"
asyncio_mode = "auto"
asyncio_default_fixture_loop_scope = "function"

[tool.coverage.report]
exclude_also = [
"if typing.TYPE_CHECKING:",
]
Empty file added tests/__init__.py
Empty file.
23 changes: 23 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import typing

import fastapi
import modern_di
import pytest
from starlette.testclient import TestClient

import modern_di_fastapi
from tests.dependencies import Dependencies


@pytest.fixture
async def app() -> fastapi.FastAPI:
app_ = fastapi.FastAPI()
container = modern_di.Container(groups=[Dependencies])
modern_di_fastapi.setup_di(app_, container=container)
return app_


@pytest.fixture
def client(app: fastapi.FastAPI) -> typing.Iterator[TestClient]:
with TestClient(app=app) as test_client:
yield test_client
33 changes: 33 additions & 0 deletions tests/dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import dataclasses

import fastapi
from modern_di import Group, Scope, providers


@dataclasses.dataclass(kw_only=True, slots=True)
class SimpleCreator:
dep1: str


@dataclasses.dataclass(kw_only=True, slots=True)
class DependentCreator:
dep1: SimpleCreator


def fetch_method_from_request(request: fastapi.Request) -> str:
assert isinstance(request, fastapi.Request)
return request.method


def fetch_url_from_websocket(websocket: fastapi.WebSocket) -> str:
assert isinstance(websocket, fastapi.WebSocket)
return websocket.url.path


class Dependencies(Group):
app_factory = providers.Factory(creator=SimpleCreator, kwargs={"dep1": "original"})
session_factory = providers.Factory(scope=Scope.SESSION, creator=DependentCreator, bound_type=None)
request_factory = providers.Factory(scope=Scope.REQUEST, creator=DependentCreator, bound_type=None)
action_factory = providers.Factory(scope=Scope.ACTION, creator=DependentCreator, bound_type=None)
request_method = providers.Factory(scope=Scope.REQUEST, creator=fetch_method_from_request, bound_type=None)
websocket_path = providers.Factory(scope=Scope.SESSION, creator=fetch_url_from_websocket, bound_type=None)
Loading
Loading