From 8eed6b78163c782e331f3ffe2b8328e93cd19f04 Mon Sep 17 00:00:00 2001 From: Automated Action Date: Thu, 12 Jun 2025 15:59:22 +0000 Subject: [PATCH] Fix circular import issue in schema files --- app/schemas/__init__.py | 4 ++++ app/schemas/author.py | 8 +++++--- app/schemas/base.py | 9 +++++++++ app/schemas/manga.py | 13 +++++++++---- app/schemas/publisher.py | 8 +++++--- 5 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 app/schemas/base.py diff --git a/app/schemas/__init__.py b/app/schemas/__init__.py index 30c1b71..edc64fb 100644 --- a/app/schemas/__init__.py +++ b/app/schemas/__init__.py @@ -1,5 +1,9 @@ +# Import schemas in order to avoid circular dependencies +# First, import schemas without relationships from app.schemas.author import Author, AuthorCreate, AuthorInDB, AuthorUpdate, AuthorWithManga from app.schemas.genre import Genre, GenreCreate, GenreInDB, GenreUpdate + +# Then, import schemas with relationships from app.schemas.manga import Manga, MangaCreate, MangaInDB, MangaUpdate, MangaWithRelations from app.schemas.publisher import ( Publisher, diff --git a/app/schemas/author.py b/app/schemas/author.py index 30ece88..1039fb9 100644 --- a/app/schemas/author.py +++ b/app/schemas/author.py @@ -1,7 +1,11 @@ from datetime import datetime +from typing import TYPE_CHECKING from pydantic import BaseModel, Field +if TYPE_CHECKING: + from app.schemas.manga import Manga + # Shared properties class AuthorBase(BaseModel): @@ -41,6 +45,4 @@ class AuthorInDB(AuthorInDBBase): # Additional properties for response with manga list class AuthorWithManga(Author): - from app.schemas.manga import Manga # Avoid circular import - - manga_list: list[Manga] = [] + manga_list: list["Manga"] = [] diff --git a/app/schemas/base.py b/app/schemas/base.py new file mode 100644 index 0000000..2c51c59 --- /dev/null +++ b/app/schemas/base.py @@ -0,0 +1,9 @@ +""" +Base schemas to avoid circular imports in Pydantic models. +This file defines common type annotations that can be reused across schema modules. +""" +from typing import TYPE_CHECKING + +# Type checking imports - these won't be evaluated at runtime +if TYPE_CHECKING: + pass \ No newline at end of file diff --git a/app/schemas/manga.py b/app/schemas/manga.py index 479eef8..feeb150 100644 --- a/app/schemas/manga.py +++ b/app/schemas/manga.py @@ -1,10 +1,15 @@ from datetime import datetime +from typing import TYPE_CHECKING, Optional from pydantic import BaseModel, Field, validator -from app.schemas.author import Author +# Use forward references for type annotations from app.schemas.genre import Genre -from app.schemas.publisher import Publisher + +# Import types for type checking only +if TYPE_CHECKING: + from app.schemas.author import Author + from app.schemas.publisher import Publisher # Shared properties @@ -75,6 +80,6 @@ class MangaInDB(MangaInDBBase): # Additional properties for response with relationships class MangaWithRelations(Manga): - author: Author | None = None - publisher: Publisher | None = None + author: Optional["Author"] = None + publisher: Optional["Publisher"] = None genres: list[Genre] = [] diff --git a/app/schemas/publisher.py b/app/schemas/publisher.py index 8bcb442..a9ece0d 100644 --- a/app/schemas/publisher.py +++ b/app/schemas/publisher.py @@ -1,7 +1,11 @@ from datetime import datetime +from typing import TYPE_CHECKING from pydantic import BaseModel, Field +if TYPE_CHECKING: + from app.schemas.manga import Manga + # Shared properties class PublisherBase(BaseModel): @@ -44,6 +48,4 @@ class PublisherInDB(PublisherInDBBase): # Additional properties for response with manga list class PublisherWithManga(Publisher): - from app.schemas.manga import Manga # Avoid circular import - - manga_list: list[Manga] = [] + manga_list: list["Manga"] = []