Skip to content

Commit 1ec98da

Browse files
committed
feat: scope Session dependency to function
1 parent 930f577 commit 1ec98da

File tree

4 files changed

+102
-35
lines changed

4 files changed

+102
-35
lines changed

.github/workflows/ci.yml

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,31 @@ on:
44
push:
55

66
jobs:
7-
87
Tests:
98
strategy:
109
matrix:
11-
tox_env: [default, sqlmodel]
10+
tox_env: [fastapi-pre121, fastapi-post121, sqlmodel]
1211
runs-on: ubuntu-latest
1312
steps:
14-
- name: 📥 checkout
15-
uses: actions/checkout@v4
16-
- name: 🔧 setup tox-uv
17-
uses: ./.github/tox-uv
18-
- name: 🧪 tox -e ${{ matrix.tox_env }}
19-
run: uv run tox -e ${{ matrix.tox_env }}
20-
- name: "🐔 codecov: upload test coverage"
21-
uses: codecov/[email protected]
22-
with:
23-
flags: ${{ matrix.tox_env }}
24-
env:
25-
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
13+
- name: 📥 checkout
14+
uses: actions/checkout@v4
15+
- name: 🔧 setup tox-uv
16+
uses: ./.github/tox-uv
17+
- name: 🧪 tox -e ${{ matrix.tox_env }}
18+
run: uv run tox -e ${{ matrix.tox_env }}
19+
- name: "🐔 codecov: upload test coverage"
20+
uses: codecov/[email protected]
21+
with:
22+
flags: ${{ matrix.tox_env }}
23+
env:
24+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
2625

2726
Ruff:
2827
runs-on: ubuntu-latest
2928
steps:
30-
- name: 📥 checkout
31-
uses: actions/checkout@v4
32-
- name: 🔧 setup uv
33-
uses: ./.github/uv
34-
- name: 🐶 ruff
35-
run: uv run ruff check
29+
- name: 📥 checkout
30+
uses: actions/checkout@v4
31+
- name: 🔧 setup uv
32+
uses: ./.github/uv
33+
- name: 🐶 ruff
34+
run: uv run ruff check

pyproject.toml

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ dev = [
7070
"aiosqlite>=0.20.0",
7171
"python-semantic-release>=9.8.8",
7272
"twine>=5.1.1",
73+
"pdbpp>=0.11.7",
7374
]
7475

7576
[tool.uv.sources]
@@ -100,15 +101,26 @@ mode = "init"
100101
changelog_file = "./docs/changelog.md"
101102

102103
[tool.tox]
103-
legacy_tox_ini = """
104-
[tox]
105-
envlist = { default, sqlmodel }
106-
107-
[testenv]
108-
passenv = CI
109-
runner = uv-venv-lock-runner
110-
commands =
111-
pytest --cov fastsqla --cov-report=term-missing --cov-report=xml
112-
extras:
113-
sqlmodel: sqlmodel
114-
"""
104+
envlist = ["sqlmodel", "fastapi-pre121", "fastapi-post121"]
105+
106+
[tool.tox.env_run_base]
107+
runner = "uv-venv-runner"
108+
commands = [
109+
[
110+
"pytest",
111+
"--cov",
112+
"fastsqla",
113+
"--cov-report=term-missing",
114+
"--cov-report=xml",
115+
],
116+
]
117+
allowlist_externals = ["pytest", "pip"]
118+
119+
[tool.tox.env.sqlmodel]
120+
extras = ["sqlmodel"]
121+
122+
[tool.tox.env.fastapi-pre121]
123+
commands_pre = [["pip", "install", "-U", "fastapi<0.121"]]
124+
125+
[tool.tox.env.fastapi-post121]
126+
commands_pre = [["pip", "install", "-U", "fastapi>=0.121"]]

src/fastsqla.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
from contextlib import _AsyncGeneratorContextManager, asynccontextmanager
55
from typing import Annotated, Generic, TypeVar, TypedDict
66

7-
from fastapi import Depends, FastAPI, Query
7+
from fastapi import Depends as BaseDepends
8+
from fastapi import FastAPI, Query
89
from pydantic import BaseModel, Field
910
from sqlalchemy import Result, Select, func, select
1011
from sqlalchemy.ext.asyncio import (
@@ -44,6 +45,15 @@
4445
logger = get_logger(__name__)
4546

4647

48+
def Depends(*args, **kwargs):
49+
"Allow backward compatibility with fastapi<0.121"
50+
try:
51+
return BaseDepends(*args, **kwargs)
52+
except TypeError:
53+
kwargs.pop("scope")
54+
return BaseDepends(*args, **kwargs)
55+
56+
4757
class Base(DeclarativeBase, DeferredReflection):
4858
"""Inherit from `Base` to declare an `SQLAlchemy` model.
4959
@@ -252,7 +262,7 @@ async def new_session() -> AsyncGenerator[AsyncSession, None]:
252262
yield session
253263

254264

255-
Session = Annotated[AsyncSession, Depends(new_session)]
265+
Session = Annotated[AsyncSession, Depends(new_session, scope="function")]
256266
"""Dependency used exclusively in endpoints to get an `SQLAlchemy` or `SQLModel` session.
257267
258268
`Session` is a [`FastAPI` dependency](https://fastapi.tiangolo.com/tutorial/dependencies/)

uv.lock

Lines changed: 47 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)