|
4 | 4 | from contextlib import _AsyncGeneratorContextManager, asynccontextmanager |
5 | 5 | from typing import Annotated, Generic, TypeVar, TypedDict |
6 | 6 |
|
7 | | -from fastapi import Depends, FastAPI, Query |
| 7 | +from fastapi import Depends as BaseDepends |
| 8 | +from fastapi import FastAPI, Query |
8 | 9 | from pydantic import BaseModel, Field |
9 | 10 | from sqlalchemy import Result, Select, func, select |
10 | 11 | from sqlalchemy.ext.asyncio import ( |
|
44 | 45 | logger = get_logger(__name__) |
45 | 46 |
|
46 | 47 |
|
| 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 | + |
47 | 57 | class Base(DeclarativeBase, DeferredReflection): |
48 | 58 | """Inherit from `Base` to declare an `SQLAlchemy` model. |
49 | 59 |
|
@@ -252,7 +262,7 @@ async def new_session() -> AsyncGenerator[AsyncSession, None]: |
252 | 262 | yield session |
253 | 263 |
|
254 | 264 |
|
255 | | -Session = Annotated[AsyncSession, Depends(new_session)] |
| 265 | +Session = Annotated[AsyncSession, Depends(new_session, scope="function")] |
256 | 266 | """Dependency used exclusively in endpoints to get an `SQLAlchemy` or `SQLModel` session. |
257 | 267 |
|
258 | 268 | `Session` is a [`FastAPI` dependency](https://fastapi.tiangolo.com/tutorial/dependencies/) |
|
0 commit comments