From 4c7dca1e691ae58b60357b9ced5402bc565baea0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Mon, 29 Apr 2024 14:52:48 -0700 Subject: [PATCH 1/4] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20types=20for?= =?UTF-8?q?=20Pydantic=202.7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sqlmodel/_compat.py | 4 +++- sqlmodel/main.py | 20 +++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/sqlmodel/_compat.py b/sqlmodel/_compat.py index 072d2b0f58..72ec8330fd 100644 --- a/sqlmodel/_compat.py +++ b/sqlmodel/_compat.py @@ -18,11 +18,13 @@ Union, ) -from pydantic import VERSION as PYDANTIC_VERSION +from pydantic import VERSION as P_VERSION from pydantic import BaseModel from pydantic.fields import FieldInfo 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.") diff --git a/sqlmodel/main.py b/sqlmodel/main.py index 9e8330d69d..3826504459 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -6,6 +6,7 @@ from enum import Enum from pathlib import Path from typing import ( + TYPE_CHECKING, AbstractSet, Any, Callable, @@ -55,6 +56,7 @@ from ._compat import ( # type: ignore[attr-defined] IS_PYDANTIC_V2, + PYDANTIC_VERSION, BaseConfig, ModelField, ModelMetaclass, @@ -80,6 +82,12 @@ ) 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") NoArgAnyCallable = Callable[[], Any] IncEx = Union[Set[int], Set[str], Dict[int, Any], Dict[str, Any], None] @@ -764,13 +772,22 @@ def model_dump( mode: Union[Literal["json", "python"], str] = "python", include: IncEx = None, exclude: IncEx = None, + context: dict[str, Any] | None = None, by_alias: bool = False, exclude_unset: bool = False, exclude_defaults: bool = False, exclude_none: 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]: + 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: return super().model_dump( mode=mode, @@ -782,6 +799,7 @@ def model_dump( exclude_none=exclude_none, round_trip=round_trip, warnings=warnings, + **extra_kwargs, ) else: return super().dict( From 83bc21604cfdcb8b9590009fd7d6c9da18d05198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Mon, 29 Apr 2024 15:03:47 -0700 Subject: [PATCH 2/4] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Update=20types=20for?= =?UTF-8?q?=20compatibility=20with=20Python=20<=203.10?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sqlmodel/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqlmodel/main.py b/sqlmodel/main.py index 3826504459..3c951a1e83 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -772,7 +772,7 @@ def model_dump( mode: Union[Literal["json", "python"], str] = "python", include: IncEx = None, exclude: IncEx = None, - context: dict[str, Any] | None = None, + context: Union[Dict[str, Any], None] = None, by_alias: bool = False, exclude_unset: bool = False, exclude_defaults: bool = False, From af5c877ae56276bf0eb0098caa9b8b768270759e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Mon, 29 Apr 2024 15:04:27 -0700 Subject: [PATCH 3/4] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Update=20types=20for?= =?UTF-8?q?=20compatibility=20with=20Python=20<=203.9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sqlmodel/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqlmodel/main.py b/sqlmodel/main.py index 3c951a1e83..409ead4972 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -782,7 +782,7 @@ def model_dump( serialize_as_any: bool = False, ) -> Dict[str, Any]: if PYDANTIC_VERSION < "2.7.0": - extra_kwargs: dict[str, Any] = { + extra_kwargs: Dict[str, Any] = { "context": context, "serialize_as_any": serialize_as_any, } From a37655f26679c5745922992993139fe542858f2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Mon, 29 Apr 2024 15:06:14 -0700 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=90=9B=20Fix=20passing=20Pydantic=20p?= =?UTF-8?q?arameters=20for=20version=202.7=20only=20in=202.7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sqlmodel/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqlmodel/main.py b/sqlmodel/main.py index 409ead4972..a16428b192 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -781,7 +781,7 @@ def model_dump( warnings: Union[bool, Literal["none", "warn", "error"]] = True, serialize_as_any: bool = False, ) -> Dict[str, Any]: - if PYDANTIC_VERSION < "2.7.0": + if PYDANTIC_VERSION >= "2.7.0": extra_kwargs: Dict[str, Any] = { "context": context, "serialize_as_any": serialize_as_any,