Skip to content

Commit b25f688

Browse files
authored
Suppress undefined annotation exception for pydantic v2 (#1069)
* Rebuild model only in case if customization present * Suppress undefined annotation exceptions
1 parent 60d5e1a commit b25f688

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

fastapi_pagination/bases.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import inspect
1515
import warnings
1616
from abc import ABC, abstractmethod
17+
from contextlib import suppress
1718
from dataclasses import dataclass
1819
from typing import (
1920
TYPE_CHECKING,
@@ -40,6 +41,14 @@
4041
from pydantic.generics import GenericModel
4142

4243

44+
try:
45+
from pydantic import PydanticUndefinedAnnotation # type: ignore[attr-defined]
46+
except ImportError:
47+
48+
class PydanticUndefinedAnnotation(Exception): # type: ignore[no-redef]
49+
pass
50+
51+
4352
from typing_extensions import Self, TypeGuard, deprecated
4453

4554
from .types import Cursor, GreaterEqualZero, ParamsType
@@ -158,7 +167,10 @@ def __pydantic_init_subclass__(cls, **kwargs: Any) -> None:
158167
for name, alias in cls.__model_aliases__.items():
159168
cls.model_fields[name].serialization_alias = alias
160169

161-
cls.model_rebuild(force=True)
170+
# rebuild model only in case if customizations is present
171+
if cls.__model_exclude__ or cls.__model_aliases__:
172+
with suppress(PydanticUndefinedAnnotation):
173+
cls.model_rebuild(force=True)
162174

163175
def __init_subclass__(cls, **kwargs: Any) -> None:
164176
try:

0 commit comments

Comments
 (0)