Fix circular import issue in schema files

This commit is contained in:
Automated Action 2025-06-12 15:59:22 +00:00
parent 6a5649a49c
commit 8eed6b7816
5 changed files with 32 additions and 10 deletions

View File

@ -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,

View File

@ -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"] = []

9
app/schemas/base.py Normal file
View File

@ -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

View File

@ -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] = []

View File

@ -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"] = []