Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress undefined annotation exception for pydantic v2 #1069

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading