Skip to content

Commit

Permalink
chore: migrate pydantic from v1 to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
iktakahiro committed Jul 26, 2024
1 parent dc00793 commit 8ccce6c
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 34 deletions.
19 changes: 9 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
POETRY=poetry
PYTEST=$(POETRY) run pytest
MYPY=$(POETRY) run mypy --ignore-missing-imports
BLACK=$(POETRY) run black
ISORT=$(POETRY) run isort
PYLINT=$(POETRY) run pylint
UVICORN=$(POETRY) run uvicorn
RYE=rye
PYTEST=$(RYE) run pytest
MYPY=$(RYE) run mypy --ignore-missing-imports
BLACK=$(RYE) run black
ISORT=$(RYE) run isort
PYLINT=$(RYE) run pylint
UVICORN=$(RYE) run uvicorn
PACKAGE=dddpy

install:
$(POETRY) install
$(RYE) sync
$(POETRY_EXPORT)

update:
$(POETRY) update
$(POETRY_EXPORT)
$(POETRY) sync

test: install
$(MYPY) main.py ./${PACKAGE}/
Expand Down
4 changes: 4 additions & 0 deletions dddpy/domain/book/book_exception.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# -*- coding: utf-8 -*-
"""Book exception"""


class BookNotFoundError(Exception):
"""BookNotFoundError is an error that occurs when a book is not found."""

message = "The book you spcecified does not exist."

def __str__(self):
Expand All @@ -11,6 +13,7 @@ def __str__(self):

class BooksNotFoundError(Exception):
"""BooksNotFoundError is an error that occurs when books are not found."""

message = "No books were found."

def __str__(self):
Expand All @@ -19,6 +22,7 @@ def __str__(self):

class BookIsbnAlreadyExistsError(Exception):
"""BookIsbnAlreadyExistsError is an error that occurs when a book with the same ISBN code already exists."""

message = "The book with the ISBN code you specified already exists."

def __str__(self):
Expand Down
2 changes: 1 addition & 1 deletion dddpy/domain/book/book_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""Book repository"""

from abc import ABC, abstractmethod
from typing import Optional
from typing import Optional

from dddpy.domain.book import Book

Expand Down
3 changes: 1 addition & 2 deletions dddpy/infrastructure/sqlite/book/book_dto.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from datetime import datetime

from sqlalchemy import String
from sqlalchemy import String
from sqlalchemy.orm import Mapped, mapped_column


from dddpy.domain.book import Book, Isbn
from dddpy.infrastructure.sqlite.database import Base
from dddpy.usecase.book import BookReadModel
Expand Down
6 changes: 3 additions & 3 deletions dddpy/presentation/schema/book/book_error_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@


class ErrorMessageBookNotFound(BaseModel):
detail: str = Field(example=BookNotFoundError.message)
detail: str = Field(examples=[BookNotFoundError.message])


class ErrorMessageBooksNotFound(BaseModel):
detail: str = Field(example=BooksNotFoundError.message)
detail: str = Field(examples=[BooksNotFoundError.message])


class ErrorMessageBookIsbnAlreadyExists(BaseModel):
detail: str = Field(example=BookIsbnAlreadyExistsError.message)
detail: str = Field(examples=[BookIsbnAlreadyExistsError.message])
12 changes: 6 additions & 6 deletions dddpy/usecase/book/book_command_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
class BookCreateModel(BaseModel):
"""BookCreateModel represents a write model to create a book."""

isbn: str = Field(example="978-0321125217")
isbn: str = Field(examples=["978-0321125217"])
title: str = Field(
example="Domain-Driven Design: Tackling Complexity in the Heart of Softwares"
examples=["Domain-Driven Design: Tackling Complexity in the Heart of Softwares"]
)
page: int = Field(ge=0, example=320)
page: int = Field(ge=0, examples=[320])


class BookUpdateModel(BaseModel):
"""BookUpdateModel represents a write model to update a book."""

title: str = Field(
example="Domain-Driven Design: Tackling Complexity in the Heart of Softwares"
examples=["Domain-Driven Design: Tackling Complexity in the Heart of Softwares"]
)
page: int = Field(ge=0, example=320)
read_page: int = Field(ge=0, example=120)
page: int = Field(ge=0, examples=[320])
read_page: int = Field(ge=0, examples=[120])

@validator("read_page")
def _validate_read_page(cls, v, values, **kwargs):
Expand Down
8 changes: 6 additions & 2 deletions dddpy/usecase/book/book_command_usecase.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def create_book(self, data: BookCreateModel) -> Optional[BookReadModel]:
raise NotImplementedError

@abstractmethod
def update_book(self, book_id: str, data: BookUpdateModel) -> Optional[BookReadModel]:
def update_book(
self, book_id: str, data: BookUpdateModel
) -> Optional[BookReadModel]:
raise NotImplementedError

@abstractmethod
Expand Down Expand Up @@ -78,7 +80,9 @@ def create_book(self, data: BookCreateModel) -> Optional[BookReadModel]:

return BookReadModel.from_entity(cast(Book, created_book))

def update_book(self, book_id: str, data: BookUpdateModel) -> Optional[BookReadModel]:
def update_book(
self, book_id: str, data: BookUpdateModel
) -> Optional[BookReadModel]:
try:
existing_book = self.uow.book_repository.find_by_id(book_id)
if existing_book is None:
Expand Down
14 changes: 7 additions & 7 deletions dddpy/usecase/book/book_query_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
class BookReadModel(BaseModel):
"""BookReadModel represents data structure as a read model."""

id: str = Field(example="vytxeTZskVKR7C7WgdSP3d")
isbn: str = Field(example="978-0321125217")
id: str = Field(examples=["vytxeTZskVKR7C7WgdSP3d"])
isbn: str = Field(examples=["978-0321125217"])
title: str = Field(
example="Domain-Driven Design: Tackling Complexity in the Heart of Softwares"
examples=["Domain-Driven Design: Tackling Complexity in the Heart of Softwares"]
)
page: int = Field(ge=0, example=320)
read_page: int = Field(ge=0, example=120)
created_at: int = Field(example=1136214245000)
updated_at: int = Field(example=1136214245000)
page: int = Field(ge=0, examples=[320])
read_page: int = Field(ge=0, examples=[120])
created_at: int = Field(examples=[1136214245000])
updated_at: int = Field(examples=[1136214245000])

class Config:
orm_mode = True
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description = "An example of Python FastAPI Domain-Driven Design and Onion Archi
authors = [{ name = "iktakahiro", email = "takahiro.ikeuchi@gmail.com" }]
dependencies = [
"sqlalchemy==2.0.9",
"pydantic==1.10.7",
"pydantic==2.8.2",
"fastapi==0.111.1",
"uvicorn==0.30.3",
"shortuuid==1.0.13",
Expand Down
7 changes: 6 additions & 1 deletion requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# universal: false

-e file:.
annotated-types==0.7.0
# via pydantic
anyio==4.4.0
# via httpx
# via starlette
Expand Down Expand Up @@ -77,9 +79,11 @@ platformdirs==4.2.2
# via pylint
pluggy==1.5.0
# via pytest
pydantic==1.10.7
pydantic==2.8.2
# via dddpy
# via fastapi
pydantic-core==2.20.1
# via pydantic
pygments==2.18.0
# via rich
pylint==3.2.6
Expand Down Expand Up @@ -111,6 +115,7 @@ typing-extensions==4.12.2
# via fastapi
# via mypy
# via pydantic
# via pydantic-core
# via sqlalchemy
# via typer
uvicorn==0.30.3
Expand Down
7 changes: 6 additions & 1 deletion requirements.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# universal: false

-e file:.
annotated-types==0.7.0
# via pydantic
anyio==4.4.0
# via httpx
# via starlette
Expand Down Expand Up @@ -51,9 +53,11 @@ markupsafe==2.1.5
# via jinja2
mdurl==0.1.2
# via markdown-it-py
pydantic==1.10.7
pydantic==2.8.2
# via dddpy
# via fastapi
pydantic-core==2.20.1
# via pydantic
pygments==2.18.0
# via rich
python-dotenv==1.0.1
Expand All @@ -80,6 +84,7 @@ typer==0.12.3
typing-extensions==4.12.2
# via fastapi
# via pydantic
# via pydantic-core
# via sqlalchemy
# via typer
uvicorn==0.30.3
Expand Down

0 comments on commit 8ccce6c

Please sign in to comment.