Skip to content

Commit

Permalink
Suppress undefined annotation exception for pydantic v2 (#1069)
Browse files Browse the repository at this point in the history
* Rebuild model only in case if customization present

* Suppress undefined annotation exceptions
  • Loading branch information
uriyyo committed Mar 5, 2024
1 parent 60d5e1a commit b25f688
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion fastapi_pagination/bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -158,7 +167,10 @@ 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__:
with suppress(PydanticUndefinedAnnotation):
cls.model_rebuild(force=True)

def __init_subclass__(cls, **kwargs: Any) -> None:
try:
Expand Down

0 comments on commit b25f688

Please sign in to comment.