From 04b336640ca1f4d4078fc0662d828b587460ed1d Mon Sep 17 00:00:00 2001 From: dbejo Date: Wed, 2 Apr 2025 18:45:17 +0200 Subject: [PATCH 1/3] Use lifespan events instead of @app.on_event annotation in sql example code --- docs_src/sql_databases/tutorial001.py | 10 ++++++---- docs_src/sql_databases/tutorial001_an.py | 10 ++++++---- docs_src/sql_databases/tutorial001_an_py310.py | 10 ++++++---- docs_src/sql_databases/tutorial001_an_py39.py | 11 +++++++---- docs_src/sql_databases/tutorial001_py310.py | 11 +++++++---- docs_src/sql_databases/tutorial001_py39.py | 11 ++++++----- docs_src/sql_databases/tutorial002.py | 10 ++++++---- docs_src/sql_databases/tutorial002_an.py | 10 +++++++--- docs_src/sql_databases/tutorial002_an_py310.py | 10 +++++++--- docs_src/sql_databases/tutorial002_an_py39.py | 10 +++++++--- docs_src/sql_databases/tutorial002_py310.py | 11 +++++++---- docs_src/sql_databases/tutorial002_py39.py | 10 ++++++---- 12 files changed, 78 insertions(+), 46 deletions(-) diff --git a/docs_src/sql_databases/tutorial001.py b/docs_src/sql_databases/tutorial001.py index be86ec0ee..3e824255b 100644 --- a/docs_src/sql_databases/tutorial001.py +++ b/docs_src/sql_databases/tutorial001.py @@ -1,3 +1,4 @@ +from contextlib import asynccontextmanager from typing import List, Union from fastapi import Depends, FastAPI, HTTPException, Query @@ -27,12 +28,13 @@ def get_session(): yield session -app = FastAPI() +@asynccontextmanager +async def lifespan(app: FastAPI): + create_db_and_tables() + yield -@app.on_event("startup") -def on_startup(): - create_db_and_tables() +app = FastAPI(lifespan=lifespan) @app.post("/heroes/") diff --git a/docs_src/sql_databases/tutorial001_an.py b/docs_src/sql_databases/tutorial001_an.py index 8c000d31c..2ef8b0299 100644 --- a/docs_src/sql_databases/tutorial001_an.py +++ b/docs_src/sql_databases/tutorial001_an.py @@ -1,3 +1,4 @@ +from contextlib import asynccontextmanager from typing import List, Union from fastapi import Depends, FastAPI, HTTPException, Query @@ -30,12 +31,13 @@ def get_session(): SessionDep = Annotated[Session, Depends(get_session)] -app = FastAPI() - -@app.on_event("startup") -def on_startup(): +@asynccontextmanager +async def lifespan(app: FastAPI): create_db_and_tables() + yield + +app = FastAPI(lifespan=lifespan) @app.post("/heroes/") diff --git a/docs_src/sql_databases/tutorial001_an_py310.py b/docs_src/sql_databases/tutorial001_an_py310.py index de1fb81fa..e19315b80 100644 --- a/docs_src/sql_databases/tutorial001_an_py310.py +++ b/docs_src/sql_databases/tutorial001_an_py310.py @@ -1,3 +1,4 @@ +from contextlib import asynccontextmanager from typing import Annotated from fastapi import Depends, FastAPI, HTTPException, Query @@ -29,12 +30,13 @@ def get_session(): SessionDep = Annotated[Session, Depends(get_session)] -app = FastAPI() - -@app.on_event("startup") -def on_startup(): +@asynccontextmanager +async def lifespan(app: FastAPI): create_db_and_tables() + yield + +app = FastAPI(lifespan=lifespan) @app.post("/heroes/") diff --git a/docs_src/sql_databases/tutorial001_an_py39.py b/docs_src/sql_databases/tutorial001_an_py39.py index 595892746..770151ec4 100644 --- a/docs_src/sql_databases/tutorial001_an_py39.py +++ b/docs_src/sql_databases/tutorial001_an_py39.py @@ -1,3 +1,4 @@ +from contextlib import asynccontextmanager from typing import Annotated, Union from fastapi import Depends, FastAPI, HTTPException, Query @@ -29,12 +30,14 @@ def get_session(): SessionDep = Annotated[Session, Depends(get_session)] -app = FastAPI() - -@app.on_event("startup") -def on_startup(): +@asynccontextmanager +async def lifespan(app: FastAPI): create_db_and_tables() + yield + + +app = FastAPI(lifespan=lifespan) @app.post("/heroes/") diff --git a/docs_src/sql_databases/tutorial001_py310.py b/docs_src/sql_databases/tutorial001_py310.py index b58462e6a..a38000caf 100644 --- a/docs_src/sql_databases/tutorial001_py310.py +++ b/docs_src/sql_databases/tutorial001_py310.py @@ -1,3 +1,5 @@ +from contextlib import asynccontextmanager + from fastapi import Depends, FastAPI, HTTPException, Query from sqlmodel import Field, Session, SQLModel, create_engine, select @@ -25,12 +27,13 @@ def get_session(): yield session -app = FastAPI() +@asynccontextmanager +async def lifespan(app: FastAPI): + create_db_and_tables() + yield -@app.on_event("startup") -def on_startup(): - create_db_and_tables() +app = FastAPI(lifespan=lifespan) @app.post("/heroes/") diff --git a/docs_src/sql_databases/tutorial001_py39.py b/docs_src/sql_databases/tutorial001_py39.py index 410a52d0c..3187235e0 100644 --- a/docs_src/sql_databases/tutorial001_py39.py +++ b/docs_src/sql_databases/tutorial001_py39.py @@ -1,3 +1,4 @@ +from contextlib import asynccontextmanager from typing import Union from fastapi import Depends, FastAPI, HTTPException, Query @@ -27,12 +28,12 @@ def get_session(): yield session -app = FastAPI() - - -@app.on_event("startup") -def on_startup(): +@asynccontextmanager +async def lifespan(app: FastAPI): create_db_and_tables() + yield + +app = FastAPI(lifespan=lifespan) @app.post("/heroes/") diff --git a/docs_src/sql_databases/tutorial002.py b/docs_src/sql_databases/tutorial002.py index 4350d19c6..e42a3a75f 100644 --- a/docs_src/sql_databases/tutorial002.py +++ b/docs_src/sql_databases/tutorial002.py @@ -1,3 +1,4 @@ +from contextlib import asynccontextmanager from typing import List, Union from fastapi import Depends, FastAPI, HTTPException, Query @@ -44,12 +45,13 @@ def get_session(): yield session -app = FastAPI() +@asynccontextmanager +async def lifespan(app: FastAPI): + create_db_and_tables() + yield -@app.on_event("startup") -def on_startup(): - create_db_and_tables() +app = FastAPI(lifespan=lifespan) @app.post("/heroes/", response_model=HeroPublic) diff --git a/docs_src/sql_databases/tutorial002_an.py b/docs_src/sql_databases/tutorial002_an.py index 15e3d7c3a..72570196b 100644 --- a/docs_src/sql_databases/tutorial002_an.py +++ b/docs_src/sql_databases/tutorial002_an.py @@ -1,3 +1,4 @@ +from contextlib import asynccontextmanager from typing import List, Union from fastapi import Depends, FastAPI, HTTPException, Query @@ -46,12 +47,15 @@ def get_session(): SessionDep = Annotated[Session, Depends(get_session)] -app = FastAPI() -@app.on_event("startup") -def on_startup(): +@asynccontextmanager +async def lifespan(app: FastAPI): create_db_and_tables() + yield + + +app = FastAPI(lifespan=lifespan) @app.post("/heroes/", response_model=HeroPublic) diff --git a/docs_src/sql_databases/tutorial002_an_py310.py b/docs_src/sql_databases/tutorial002_an_py310.py index 64c554b8a..3baa99666 100644 --- a/docs_src/sql_databases/tutorial002_an_py310.py +++ b/docs_src/sql_databases/tutorial002_an_py310.py @@ -1,3 +1,4 @@ +from contextlib import asynccontextmanager from typing import Annotated from fastapi import Depends, FastAPI, HTTPException, Query @@ -45,12 +46,15 @@ def get_session(): SessionDep = Annotated[Session, Depends(get_session)] -app = FastAPI() -@app.on_event("startup") -def on_startup(): +@asynccontextmanager +async def lifespan(app: FastAPI): create_db_and_tables() + yield + + +app = FastAPI(lifespan=lifespan) @app.post("/heroes/", response_model=HeroPublic) diff --git a/docs_src/sql_databases/tutorial002_an_py39.py b/docs_src/sql_databases/tutorial002_an_py39.py index a8a0721ff..4ff0628b4 100644 --- a/docs_src/sql_databases/tutorial002_an_py39.py +++ b/docs_src/sql_databases/tutorial002_an_py39.py @@ -1,3 +1,4 @@ +from contextlib import asynccontextmanager from typing import Annotated, Union from fastapi import Depends, FastAPI, HTTPException, Query @@ -45,12 +46,15 @@ def get_session(): SessionDep = Annotated[Session, Depends(get_session)] -app = FastAPI() -@app.on_event("startup") -def on_startup(): +@asynccontextmanager +async def lifespan(app: FastAPI): create_db_and_tables() + yield + + +app = FastAPI(lifespan=lifespan) @app.post("/heroes/", response_model=HeroPublic) diff --git a/docs_src/sql_databases/tutorial002_py310.py b/docs_src/sql_databases/tutorial002_py310.py index ec3d68db5..c2efe088c 100644 --- a/docs_src/sql_databases/tutorial002_py310.py +++ b/docs_src/sql_databases/tutorial002_py310.py @@ -1,3 +1,5 @@ +from contextlib import asynccontextmanager + from fastapi import Depends, FastAPI, HTTPException, Query from sqlmodel import Field, Session, SQLModel, create_engine, select @@ -42,12 +44,13 @@ def get_session(): yield session -app = FastAPI() +@asynccontextmanager +async def lifespan(app: FastAPI): + create_db_and_tables() + yield -@app.on_event("startup") -def on_startup(): - create_db_and_tables() +app = FastAPI(lifespan=lifespan) @app.post("/heroes/", response_model=HeroPublic) diff --git a/docs_src/sql_databases/tutorial002_py39.py b/docs_src/sql_databases/tutorial002_py39.py index d8f5dd090..74c70d0f6 100644 --- a/docs_src/sql_databases/tutorial002_py39.py +++ b/docs_src/sql_databases/tutorial002_py39.py @@ -1,3 +1,4 @@ +from contextlib import asynccontextmanager from typing import Union from fastapi import Depends, FastAPI, HTTPException, Query @@ -44,12 +45,13 @@ def get_session(): yield session -app = FastAPI() +@asynccontextmanager +async def lifespan(app: FastAPI): + create_db_and_tables() + yield -@app.on_event("startup") -def on_startup(): - create_db_and_tables() +app = FastAPI(lifespan=lifespan) @app.post("/heroes/", response_model=HeroPublic) From c5fe422f1b499bdd0b5976163a7d66bcb510f9a9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 2 Apr 2025 16:51:13 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20for?= =?UTF-8?q?mat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs_src/sql_databases/tutorial001_an.py | 1 + docs_src/sql_databases/tutorial001_an_py310.py | 1 + docs_src/sql_databases/tutorial001_py39.py | 1 + 3 files changed, 3 insertions(+) diff --git a/docs_src/sql_databases/tutorial001_an.py b/docs_src/sql_databases/tutorial001_an.py index 2ef8b0299..9e492546e 100644 --- a/docs_src/sql_databases/tutorial001_an.py +++ b/docs_src/sql_databases/tutorial001_an.py @@ -37,6 +37,7 @@ async def lifespan(app: FastAPI): create_db_and_tables() yield + app = FastAPI(lifespan=lifespan) diff --git a/docs_src/sql_databases/tutorial001_an_py310.py b/docs_src/sql_databases/tutorial001_an_py310.py index e19315b80..2054877cb 100644 --- a/docs_src/sql_databases/tutorial001_an_py310.py +++ b/docs_src/sql_databases/tutorial001_an_py310.py @@ -36,6 +36,7 @@ async def lifespan(app: FastAPI): create_db_and_tables() yield + app = FastAPI(lifespan=lifespan) diff --git a/docs_src/sql_databases/tutorial001_py39.py b/docs_src/sql_databases/tutorial001_py39.py index 3187235e0..22de0dd53 100644 --- a/docs_src/sql_databases/tutorial001_py39.py +++ b/docs_src/sql_databases/tutorial001_py39.py @@ -33,6 +33,7 @@ async def lifespan(app: FastAPI): create_db_and_tables() yield + app = FastAPI(lifespan=lifespan) From 23c4f42c5065c025416a76caea6ee954a536b4d4 Mon Sep 17 00:00:00 2001 From: dbejo Date: Thu, 3 Apr 2025 08:45:16 +0200 Subject: [PATCH 3/3] Format with script --- docs_src/sql_databases/tutorial001_an.py | 1 + docs_src/sql_databases/tutorial001_an_py310.py | 1 + docs_src/sql_databases/tutorial001_py39.py | 1 + 3 files changed, 3 insertions(+) diff --git a/docs_src/sql_databases/tutorial001_an.py b/docs_src/sql_databases/tutorial001_an.py index 2ef8b0299..9e492546e 100644 --- a/docs_src/sql_databases/tutorial001_an.py +++ b/docs_src/sql_databases/tutorial001_an.py @@ -37,6 +37,7 @@ async def lifespan(app: FastAPI): create_db_and_tables() yield + app = FastAPI(lifespan=lifespan) diff --git a/docs_src/sql_databases/tutorial001_an_py310.py b/docs_src/sql_databases/tutorial001_an_py310.py index e19315b80..2054877cb 100644 --- a/docs_src/sql_databases/tutorial001_an_py310.py +++ b/docs_src/sql_databases/tutorial001_an_py310.py @@ -36,6 +36,7 @@ async def lifespan(app: FastAPI): create_db_and_tables() yield + app = FastAPI(lifespan=lifespan) diff --git a/docs_src/sql_databases/tutorial001_py39.py b/docs_src/sql_databases/tutorial001_py39.py index 3187235e0..22de0dd53 100644 --- a/docs_src/sql_databases/tutorial001_py39.py +++ b/docs_src/sql_databases/tutorial001_py39.py @@ -33,6 +33,7 @@ async def lifespan(app: FastAPI): create_db_and_tables() yield + app = FastAPI(lifespan=lifespan)