Skip to content

Commit 0402496

Browse files
authored
šŸ› Fix attribute handling in model_dump for compatibility with the latest Pydantic versions (#1595)
1 parent 39d8c2c commit 0402496

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

ā€Žsqlmodel/main.pyā€Ž

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ def __dataclass_transform__(
109109
return lambda a: a
110110

111111

112-
class FieldInfo(PydanticFieldInfo):
112+
class FieldInfo(PydanticFieldInfo): # type: ignore[misc]
113+
# mypy - ignore that PydanticFieldInfo is @final
113114
def __init__(self, default: Any = Undefined, **kwargs: Any) -> None:
114115
primary_key = kwargs.pop("primary_key", False)
115116
nullable = kwargs.pop("nullable", Undefined)
@@ -863,27 +864,27 @@ def model_dump(
863864
mode: Union[Literal["json", "python"], str] = "python",
864865
include: Union[IncEx, None] = None,
865866
exclude: Union[IncEx, None] = None,
866-
context: Union[Any, None] = None,
867+
context: Union[Any, None] = None, # v2.7
867868
by_alias: Union[bool, None] = None,
868869
exclude_unset: bool = False,
869870
exclude_defaults: bool = False,
870871
exclude_none: bool = False,
872+
exclude_computed_fields: bool = False, # v2.12
871873
round_trip: bool = False,
872874
warnings: Union[bool, Literal["none", "warn", "error"]] = True,
873-
fallback: Union[Callable[[Any], Any], None] = None,
874-
serialize_as_any: bool = False,
875+
fallback: Union[Callable[[Any], Any], None] = None, # v2.11
876+
serialize_as_any: bool = False, # v2.7
875877
) -> Dict[str, Any]:
876878
if PYDANTIC_MINOR_VERSION < (2, 11):
877879
by_alias = by_alias or False
880+
extra_kwargs: Dict[str, Any] = {}
878881
if PYDANTIC_MINOR_VERSION >= (2, 7):
879-
extra_kwargs: Dict[str, Any] = {
880-
"context": context,
881-
"serialize_as_any": serialize_as_any,
882-
}
882+
extra_kwargs["context"] = context
883+
extra_kwargs["serialize_as_any"] = serialize_as_any
883884
if PYDANTIC_MINOR_VERSION >= (2, 11):
884885
extra_kwargs["fallback"] = fallback
885-
else:
886-
extra_kwargs = {}
886+
if PYDANTIC_MINOR_VERSION >= (2, 12):
887+
extra_kwargs["exclude_computed_fields"] = exclude_computed_fields
887888
if IS_PYDANTIC_V2:
888889
return super().model_dump(
889890
mode=mode,

0 commit comments

Comments
Ā (0)