[Python] Why am I getting 'type[Category] instead of Category?

Discussão em 'Python' iniciado por Stack, Agosto 21, 2025.

  1. Stack

    Stack Membro Participativo

    I'm playing with FastAIPI/sqlalchemy. Repository is working correctly, I can CRUD from swagger and everything is being displayed in .sqlite file. Problem I have is that methods that return fetched item from db (read_one, update_one and delete_one), shows warning on return value "Expected type 'Category', got 'type[Category]' instead".

    from dataclasses import dataclass
    from typing import Sequence

    from sqlalchemy import select
    from sqlalchemy.orm import Session

    from app.models.coordinate import Category
    from app.repositories.errors import DoesNotExistError, ExistError
    from app.repositories.model import Repository
    from app.schemas.coordinates import CategoryCreate, CategoryUpdate


    @dataclass(frozen=True)
    class CategorySqliteRepository(
    Repository[
    Category,
    CategoryCreate,
    CategoryUpdate,
    ]
    ):
    sqlite: Session

    def read_one(self, item_id: int) -> Category:
    category = self.sqlite.get(Category, item_id)

    if category is None:
    raise DoesNotExistError(resource="category", id=str(item_id))

    return category

    def read_all(self, skip: int = 0, limit: int = 100) -> Sequence[Category]:
    return self.sqlite.scalars(select(Category).offset(skip).limit(limit)).all()

    def create_one(self, item: CategoryCreate) -> Category:
    data = item.model_dump()
    category = Category(**data)

    existing_category = self.sqlite.scalars(
    select(Category).filter(Category.name == category.name)
    ).all()
    if existing_category:
    raise ExistError(resource="category", name=category.name)

    self.sqlite.add(category)
    self.sqlite.commit()
    self.sqlite.refresh(category)

    return category

    def update_one(self, item_id: int, item: CategoryUpdate) -> Category:
    category = self.sqlite.get(Category, item_id)

    if category is None:
    raise DoesNotExistError(resource="category", id=str(item_id))

    data = item.model_dump(exclude_unset=True)
    existing_category = self.sqlite.scalars(
    select(Category).filter(Category.name == data["name"])
    ).all()
    if existing_category:
    raise ExistError(resource="category", name=data["name"])

    for key, value in data.items():
    setattr(category, key, value)
    self.sqlite.add(category)
    self.sqlite.commit()
    self.sqlite.refresh(category)

    return category

    def delete_one(self, item_id: int) -> Category:
    category = self.sqlite.get(Category, item_id)

    if category is None:
    raise DoesNotExistError(resource="category", id=str(item_id))

    self.sqlite.delete(category)
    self.sqlite.commit()

    return category



    from typing import List

    from sqlalchemy import ForeignKey, Integer, String
    from sqlalchemy.orm import (
    Mapped,
    mapped_column,
    relationship,
    )

    from app.runner.database import Base


    class Category(Base):
    __tablename__ = "category"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    name: Mapped[str] = mapped_column(String, unique=True, nullable=False)

    coordinates: Mapped[List["Coordinate"]] = relationship(
    back_populates="category", cascade="all, delete-orphan"
    )



    from pydantic import BaseModel, ConfigDict


    class CategoryBase(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    name: str


    class CategoryCreate(CategoryBase):
    pass


    class CategoryPublic(CategoryBase):
    id: int


    class CategoryUpdate(BaseModel):
    name: str | None = None


    class CoordinatePublicWithCategory(CoordinatePublic):
    model_config = ConfigDict(from_attributes=True)

    category: CategoryPublic | None = None


    I tried searching the error and a help of AI. Couldn't find the way to fix it.

    Continue reading...

Compartilhe esta Página