Skip to content

Commit 1b435da

Browse files
committed
Use pydantic v2
1 parent ee19398 commit 1b435da

File tree

5 files changed

+538
-339
lines changed

5 files changed

+538
-339
lines changed

config.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import logging
22
import os
3-
from typing import List, Literal
3+
from typing import Dict, List, Literal
44

55
import structlog
66
from opentelemetry import trace
7-
from pydantic import BaseSettings
7+
8+
# TODO: update this to use the new V2 pydantic-settings
9+
from pydantic.v1 import BaseSettings
810
from sqlalchemy_bind_manager import SQLAlchemyAsyncConfig
911
from structlog.typing import Processor
1012

1113
TYPE_ENVIRONMENT = Literal["local", "test", "staging", "production"]
1214

1315

1416
class AppConfig(BaseSettings):
15-
SQLALCHEMY_CONFIG = {
16-
"default": SQLAlchemyAsyncConfig(
17+
SQLALCHEMY_CONFIG: Dict[str, SQLAlchemyAsyncConfig] = dict(
18+
default=SQLAlchemyAsyncConfig(
1719
engine_url=f"sqlite+aiosqlite:///{os.path.dirname(os.path.abspath(__file__))}/sqlite.db",
1820
engine_options=dict(
1921
connect_args={
@@ -23,7 +25,7 @@ class AppConfig(BaseSettings):
2325
future=True,
2426
),
2527
),
26-
}
28+
)
2729
ENVIRONMENT: TYPE_ENVIRONMENT = "local"
2830
DEBUG: bool = False
2931

domains/books/_service.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ async def create_book(self, book: BookData) -> Book:
2929
some_cpu_intensive_blocking_task, book.dict()
3030
)
3131
book_model = BookModel(**book_data_altered)
32-
return Book.from_orm(await self.book_repository.save(book_model))
32+
return Book.model_validate(
33+
await self.book_repository.save(book_model), from_attributes=True
34+
)
3335

3436
async def list_books(self) -> Iterable[Book]:
3537
books = await self.book_repository.find()
36-
return [Book.from_orm(x) for x in books]
38+
return [Book.model_validate(x, from_attributes=True) for x in books]
3739

3840

3941
def some_cpu_intensive_blocking_task(book: dict) -> dict:

0 commit comments

Comments
 (0)