From a20d15ba0a9ecae066818259600980fbc8e9c94e Mon Sep 17 00:00:00 2001 From: Andrew Grangaard Date: Tue, 7 Oct 2025 21:35:05 -0700 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20=20Fix=20reversion=20i?= =?UTF-8?q?n=20model=5Fdump=20attribute=20handling=20for=20pydantic=20v2.7?= =?UTF-8?q?=20..=20v2.10?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit + The else block added to fix linting errors (4b5ad42) for #1340 was obliterating the 'context' and 'serialize_as_any' attributes for versions >= 2.7 and < 2.11. + Fix by initializing extra_kwargs and then setting on a per-version level --- sqlmodel/main.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/sqlmodel/main.py b/sqlmodel/main.py index 38c85915aa..739a274933 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -863,27 +863,24 @@ def model_dump( mode: Union[Literal["json", "python"], str] = "python", include: Union[IncEx, None] = None, exclude: Union[IncEx, None] = None, - context: Union[Any, None] = None, + context: Union[Any, None] = None, # v2.7 by_alias: Union[bool, None] = None, exclude_unset: bool = False, exclude_defaults: bool = False, exclude_none: bool = False, round_trip: bool = False, warnings: Union[bool, Literal["none", "warn", "error"]] = True, - fallback: Union[Callable[[Any], Any], None] = None, - serialize_as_any: bool = False, + fallback: Union[Callable[[Any], Any], None] = None, # v2.11 + serialize_as_any: bool = False, # v2.7 ) -> Dict[str, Any]: if PYDANTIC_MINOR_VERSION < (2, 11): by_alias = by_alias or False + extra_kwargs: Dict[str, Any] = {} if PYDANTIC_MINOR_VERSION >= (2, 7): - extra_kwargs: Dict[str, Any] = { - "context": context, - "serialize_as_any": serialize_as_any, - } + extra_kwargs["context"] = context + extra_kwargs["serialize_as_any"] = serialize_as_any if PYDANTIC_MINOR_VERSION >= (2, 11): extra_kwargs["fallback"] = fallback - else: - extra_kwargs = {} if IS_PYDANTIC_V2: return super().model_dump( mode=mode, From c9765b3c07a2db79b53800da75a17080d836ca06 Mon Sep 17 00:00:00 2001 From: Andrew Grangaard Date: Tue, 7 Oct 2025 21:39:16 -0700 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=92=9A=20Fix=20linting=20from=20overr?= =?UTF-8?q?iding=20@final=20FieldInfo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sqlmodel/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sqlmodel/main.py b/sqlmodel/main.py index 739a274933..66a619caaa 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -109,7 +109,8 @@ def __dataclass_transform__( return lambda a: a -class FieldInfo(PydanticFieldInfo): +class FieldInfo(PydanticFieldInfo): # type: ignore[misc] + # mypy - ignore that PydanticFieldInfo is @final def __init__(self, default: Any = Undefined, **kwargs: Any) -> None: primary_key = kwargs.pop("primary_key", False) nullable = kwargs.pop("nullable", Undefined) From bda621ab693f90034ac8d4c6a5e906e1d4af706a Mon Sep 17 00:00:00 2001 From: Andrew Grangaard Date: Tue, 7 Oct 2025 21:40:49 -0700 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=92=9A=20Add=20exclude=5Fcomputed=5Ff?= =?UTF-8?q?ields=20from=20pydantic=202.12?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit + match the upstream interface to avoid mypy linting warning. + pydantic added exclude_computed_fields in pydantic/pydantic-core#1780 --- sqlmodel/main.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sqlmodel/main.py b/sqlmodel/main.py index 66a619caaa..1925a48f04 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -869,6 +869,7 @@ def model_dump( exclude_unset: bool = False, exclude_defaults: bool = False, exclude_none: bool = False, + exclude_computed_fields: bool = False, # v2.12 round_trip: bool = False, warnings: Union[bool, Literal["none", "warn", "error"]] = True, fallback: Union[Callable[[Any], Any], None] = None, # v2.11 @@ -882,6 +883,8 @@ def model_dump( extra_kwargs["serialize_as_any"] = serialize_as_any if PYDANTIC_MINOR_VERSION >= (2, 11): extra_kwargs["fallback"] = fallback + if PYDANTIC_MINOR_VERSION >= (2, 12): + extra_kwargs["exclude_computed_fields"] = exclude_computed_fields if IS_PYDANTIC_V2: return super().model_dump( mode=mode,