From 186347ed18a661ba8dc69d2954e48c2a07ca4639 Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Tue, 5 Mar 2024 14:17:46 +0200 Subject: [PATCH 1/2] Rebuild model only in case if customization present --- fastapi_pagination/bases.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fastapi_pagination/bases.py b/fastapi_pagination/bases.py index cc4a0ff0..0f076cc8 100644 --- a/fastapi_pagination/bases.py +++ b/fastapi_pagination/bases.py @@ -158,7 +158,9 @@ def __pydantic_init_subclass__(cls, **kwargs: Any) -> None: for name, alias in cls.__model_aliases__.items(): cls.model_fields[name].serialization_alias = alias - cls.model_rebuild(force=True) + # rebuild model only in case if customizations is present + if cls.__model_exclude__ or cls.__model_aliases__: + cls.model_rebuild(force=True) def __init_subclass__(cls, **kwargs: Any) -> None: try: From d88a7e95532e08aed44fa572119475ec61ef735b Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Tue, 5 Mar 2024 14:22:23 +0200 Subject: [PATCH 2/2] Suppress undefined annotation exceptions --- fastapi_pagination/bases.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/fastapi_pagination/bases.py b/fastapi_pagination/bases.py index 0f076cc8..f633dcc2 100644 --- a/fastapi_pagination/bases.py +++ b/fastapi_pagination/bases.py @@ -14,6 +14,7 @@ import inspect import warnings from abc import ABC, abstractmethod +from contextlib import suppress from dataclasses import dataclass from typing import ( TYPE_CHECKING, @@ -40,6 +41,14 @@ from pydantic.generics import GenericModel +try: + from pydantic import PydanticUndefinedAnnotation # type: ignore[attr-defined] +except ImportError: + + class PydanticUndefinedAnnotation(Exception): # type: ignore[no-redef] + pass + + from typing_extensions import Self, TypeGuard, deprecated from .types import Cursor, GreaterEqualZero, ParamsType @@ -160,7 +169,8 @@ def __pydantic_init_subclass__(cls, **kwargs: Any) -> None: # rebuild model only in case if customizations is present if cls.__model_exclude__ or cls.__model_aliases__: - cls.model_rebuild(force=True) + with suppress(PydanticUndefinedAnnotation): + cls.model_rebuild(force=True) def __init_subclass__(cls, **kwargs: Any) -> None: try: