♻️ Refactor types to properly support Pydantic 2.7 (#913)
This commit is contained in:
parent
6151f23e15
commit
2454694de3
@ -18,11 +18,13 @@ from typing import (
|
|||||||
Union,
|
Union,
|
||||||
)
|
)
|
||||||
|
|
||||||
from pydantic import VERSION as PYDANTIC_VERSION
|
from pydantic import VERSION as P_VERSION
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from pydantic.fields import FieldInfo
|
from pydantic.fields import FieldInfo
|
||||||
from typing_extensions import get_args, get_origin
|
from typing_extensions import get_args, get_origin
|
||||||
|
|
||||||
|
# Reassign variable to make it reexported for mypy
|
||||||
|
PYDANTIC_VERSION = P_VERSION
|
||||||
IS_PYDANTIC_V2 = PYDANTIC_VERSION.startswith("2.")
|
IS_PYDANTIC_V2 = PYDANTIC_VERSION.startswith("2.")
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ from decimal import Decimal
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import (
|
from typing import (
|
||||||
|
TYPE_CHECKING,
|
||||||
AbstractSet,
|
AbstractSet,
|
||||||
Any,
|
Any,
|
||||||
Callable,
|
Callable,
|
||||||
@ -55,6 +56,7 @@ from typing_extensions import Literal, deprecated, get_origin
|
|||||||
|
|
||||||
from ._compat import ( # type: ignore[attr-defined]
|
from ._compat import ( # type: ignore[attr-defined]
|
||||||
IS_PYDANTIC_V2,
|
IS_PYDANTIC_V2,
|
||||||
|
PYDANTIC_VERSION,
|
||||||
BaseConfig,
|
BaseConfig,
|
||||||
ModelField,
|
ModelField,
|
||||||
ModelMetaclass,
|
ModelMetaclass,
|
||||||
@ -80,6 +82,12 @@ from ._compat import ( # type: ignore[attr-defined]
|
|||||||
)
|
)
|
||||||
from .sql.sqltypes import GUID, AutoString
|
from .sql.sqltypes import GUID, AutoString
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from pydantic._internal._model_construction import ModelMetaclass as ModelMetaclass
|
||||||
|
from pydantic._internal._repr import Representation as Representation
|
||||||
|
from pydantic_core import PydanticUndefined as Undefined
|
||||||
|
from pydantic_core import PydanticUndefinedType as UndefinedType
|
||||||
|
|
||||||
_T = TypeVar("_T")
|
_T = TypeVar("_T")
|
||||||
NoArgAnyCallable = Callable[[], Any]
|
NoArgAnyCallable = Callable[[], Any]
|
||||||
IncEx = Union[Set[int], Set[str], Dict[int, Any], Dict[str, Any], None]
|
IncEx = Union[Set[int], Set[str], Dict[int, Any], Dict[str, Any], None]
|
||||||
@ -764,13 +772,22 @@ class SQLModel(BaseModel, metaclass=SQLModelMetaclass, registry=default_registry
|
|||||||
mode: Union[Literal["json", "python"], str] = "python",
|
mode: Union[Literal["json", "python"], str] = "python",
|
||||||
include: IncEx = None,
|
include: IncEx = None,
|
||||||
exclude: IncEx = None,
|
exclude: IncEx = None,
|
||||||
|
context: Union[Dict[str, Any], None] = None,
|
||||||
by_alias: bool = False,
|
by_alias: bool = False,
|
||||||
exclude_unset: bool = False,
|
exclude_unset: bool = False,
|
||||||
exclude_defaults: bool = False,
|
exclude_defaults: bool = False,
|
||||||
exclude_none: bool = False,
|
exclude_none: bool = False,
|
||||||
round_trip: bool = False,
|
round_trip: bool = False,
|
||||||
warnings: bool = True,
|
warnings: Union[bool, Literal["none", "warn", "error"]] = True,
|
||||||
|
serialize_as_any: bool = False,
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
|
if PYDANTIC_VERSION >= "2.7.0":
|
||||||
|
extra_kwargs: Dict[str, Any] = {
|
||||||
|
"context": context,
|
||||||
|
"serialize_as_any": serialize_as_any,
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
extra_kwargs = {}
|
||||||
if IS_PYDANTIC_V2:
|
if IS_PYDANTIC_V2:
|
||||||
return super().model_dump(
|
return super().model_dump(
|
||||||
mode=mode,
|
mode=mode,
|
||||||
@ -782,6 +799,7 @@ class SQLModel(BaseModel, metaclass=SQLModelMetaclass, registry=default_registry
|
|||||||
exclude_none=exclude_none,
|
exclude_none=exclude_none,
|
||||||
round_trip=round_trip,
|
round_trip=round_trip,
|
||||||
warnings=warnings,
|
warnings=warnings,
|
||||||
|
**extra_kwargs,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return super().dict(
|
return super().dict(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user