mirror of
https://github.com/rommapp/romm.git
synced 2026-11-30 02:22:44 +01:00
LimitOffsetPage.create is annotated to return LimitOffsetPage but builds cls, so subclass pages failed return-value checks. A shared TypedLimitOffsetPage narrows create() to Self, and resolve_params() results are annotated with each page's params class. CustomLimitOffsetPage.total keeps its intentional widening to None (with_total=false) behind a narrow assignment ignore. The OpenAPI schema is byte-identical before and after. Claude-Session: https://claude.ai/code/session_01SPpv6W2ygP9tQ61fTQxdYp Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
40 lines
1.1 KiB
Python
40 lines
1.1 KiB
Python
from collections.abc import Sequence
|
|
from datetime import datetime, timezone
|
|
from typing import Annotated, Any, Self, cast
|
|
|
|
from fastapi_pagination.bases import AbstractParams
|
|
from fastapi_pagination.limit_offset import LimitOffsetPage
|
|
from pydantic import BaseModel as PydanticBaseModel
|
|
from pydantic import PlainSerializer
|
|
|
|
|
|
def _serialize_utc_datetime(dt: datetime) -> str:
|
|
"""Serialize datetime ensuring UTC timezone is always present."""
|
|
if dt.tzinfo is None:
|
|
dt = dt.replace(tzinfo=timezone.utc)
|
|
return dt.isoformat()
|
|
|
|
|
|
UTCDatetime = Annotated[datetime, PlainSerializer(_serialize_utc_datetime)]
|
|
|
|
|
|
class BaseModel(PydanticBaseModel):
|
|
"""Base response model for all API responses."""
|
|
|
|
pass
|
|
|
|
|
|
class TypedLimitOffsetPage[T: PydanticBaseModel](LimitOffsetPage[T]):
|
|
"""LimitOffsetPage whose `create` is typed to return the subclass it builds."""
|
|
|
|
@classmethod
|
|
def create(
|
|
cls,
|
|
items: Sequence[T],
|
|
params: AbstractParams,
|
|
*,
|
|
total: int | None = None,
|
|
**kwargs: Any,
|
|
) -> Self:
|
|
return cast(Self, super().create(items, params, total=total, **kwargs))
|