Skip to content

Commit d05f2cd

Browse files
committed
migrate to pydantic 2
1 parent d07fb04 commit d05f2cd

File tree

8 files changed

+17
-23
lines changed

8 files changed

+17
-23
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ SQL_TEST_DB=testdb
66
SQL_HOST=db
77
SQL_USER=user
88
SQL_PASS=secret
9+
SQL_URL=postgresql+asyncpg://${SQL_USER}:${SQL_PASS}@${SQL_HOST}/${SQL_DB}
910

1011
ALGORITHM=HS256
1112
ACCESS_TOKEN_EXPIRE_MINUTES=30

app/api/nonsense.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async def update_nonsense(
3636
db_session: AsyncSession = Depends(get_db),
3737
):
3838
nonsense = await Nonsense.find(db_session, name)
39-
await nonsense.update(db_session, **payload.dict())
39+
await nonsense.update(db_session, **payload.model_dump())
4040
return nonsense
4141

4242

@@ -45,6 +45,6 @@ async def merge_nonsense(
4545
payload: NonsenseSchema,
4646
db_session: AsyncSession = Depends(get_db),
4747
):
48-
nonsense = Nonsense(**payload.dict())
48+
nonsense = Nonsense(**payload.model_dump())
4949
await nonsense.save_or_update(db_session)
5050
return nonsense

app/api/shakespeare.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from typing import Annotated
22

33
from fastapi import APIRouter, Depends, Query
4-
from pydantic import Required
54
from sqlalchemy.ext.asyncio import AsyncSession
65

76
from app.database import get_db
@@ -14,7 +13,7 @@
1413
"/",
1514
)
1615
async def find_paragraph(
17-
character: Annotated[str, Query(description="Character name")] = Required,
16+
character: Annotated[str, Query(description="Character name")],
1817
db_session: AsyncSession = Depends(get_db),
1918
):
2019
return await Paragraph.find(db_session=db_session, character=character)

app/api/stuff.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ async def update_stuff(
5454
db_session: AsyncSession = Depends(get_db),
5555
):
5656
stuff = await Stuff.find(db_session, name)
57-
await stuff.update(db_session, **payload.dict())
57+
await stuff.update(db_session, **payload.model_dump())
5858
return stuff

app/config.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
import os
22
from functools import lru_cache
33

4-
from pydantic import BaseSettings, PostgresDsn
4+
from pydantic import PostgresDsn
5+
from pydantic_settings import BaseSettings
56

67

78
class Settings(BaseSettings):
8-
asyncpg_url: PostgresDsn = PostgresDsn.build(
9-
scheme="postgresql+asyncpg",
10-
user=os.getenv("SQL_USER"),
11-
password=os.getenv("POSTGRES_PASSWORD"),
12-
host=os.getenv("SQL_HOST"),
13-
port="5432",
14-
path=f"/{os.getenv('SQL_DB') or ''}",
15-
)
9+
asyncpg_url: PostgresDsn = os.getenv("SQL_URL")
1610

1711

1812
@lru_cache

app/database.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
logger = AppLogger.__call__().get_logger()
1111

1212
engine = create_async_engine(
13-
global_settings.asyncpg_url,
13+
global_settings.asyncpg_url.unicode_string(),
1414
future=True,
1515
echo=True,
1616
)

app/schemas/nnonsense.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class NonsenseSchema(BaseModel):
1414
)
1515

1616
class Config:
17-
orm_mode = True
18-
schema_extra = {
17+
from_attributes = True
18+
json_schema_extra = {
1919
"example": {
2020
"name": "Name for Some Nonsense",
2121
"description": "Some Nonsense Description",
@@ -38,8 +38,8 @@ class NonsenseResponse(BaseModel):
3838
)
3939

4040
class Config:
41-
orm_mode = True
42-
schema_extra = {
41+
from_attributes = True
42+
json_schema_extra = {
4343
"example": {
4444
"config_id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
4545
"name": "Name for Some Nonsense",

app/schemas/stuff.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class StuffSchema(BaseModel):
1414
)
1515

1616
class Config:
17-
orm_mode = True
18-
schema_extra = {
17+
from_attributes = True
18+
json_schema_extra = {
1919
"example": {
2020
"name": "Name for Some Stuff",
2121
"description": "Some Stuff Description",
@@ -38,8 +38,8 @@ class StuffResponse(BaseModel):
3838
)
3939

4040
class Config:
41-
orm_mode = True
42-
schema_extra = {
41+
from_attributes = True
42+
json_schema_extra = {
4343
"example": {
4444
"config_id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
4545
"name": "Name for Some Stuff",

0 commit comments

Comments
 (0)