🐛 Fix attribute handling in model_dump for compatibility with the latest Pydantic versions (#1595)

This commit is contained in:
Andrew Grangaard
2025-10-08 03:33:47 -07:00
committed by GitHub
parent 39d8c2c0df
commit 0402496be2

View File

@ -109,7 +109,8 @@ def __dataclass_transform__(
return lambda a: a 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: def __init__(self, default: Any = Undefined, **kwargs: Any) -> None:
primary_key = kwargs.pop("primary_key", False) primary_key = kwargs.pop("primary_key", False)
nullable = kwargs.pop("nullable", Undefined) nullable = kwargs.pop("nullable", Undefined)
@ -863,27 +864,27 @@ class SQLModel(BaseModel, metaclass=SQLModelMetaclass, registry=default_registry
mode: Union[Literal["json", "python"], str] = "python", mode: Union[Literal["json", "python"], str] = "python",
include: Union[IncEx, None] = None, include: Union[IncEx, None] = None,
exclude: 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, by_alias: Union[bool, None] = None,
exclude_unset: bool = False, exclude_unset: bool = False,
exclude_defaults: bool = False, exclude_defaults: bool = False,
exclude_none: bool = False, exclude_none: bool = False,
exclude_computed_fields: bool = False, # v2.12
round_trip: bool = False, round_trip: bool = False,
warnings: Union[bool, Literal["none", "warn", "error"]] = True, warnings: Union[bool, Literal["none", "warn", "error"]] = True,
fallback: Union[Callable[[Any], Any], None] = None, fallback: Union[Callable[[Any], Any], None] = None, # v2.11
serialize_as_any: bool = False, serialize_as_any: bool = False, # v2.7
) -> Dict[str, Any]: ) -> Dict[str, Any]:
if PYDANTIC_MINOR_VERSION < (2, 11): if PYDANTIC_MINOR_VERSION < (2, 11):
by_alias = by_alias or False by_alias = by_alias or False
extra_kwargs: Dict[str, Any] = {}
if PYDANTIC_MINOR_VERSION >= (2, 7): if PYDANTIC_MINOR_VERSION >= (2, 7):
extra_kwargs: Dict[str, Any] = { extra_kwargs["context"] = context
"context": context, extra_kwargs["serialize_as_any"] = serialize_as_any
"serialize_as_any": serialize_as_any,
}
if PYDANTIC_MINOR_VERSION >= (2, 11): if PYDANTIC_MINOR_VERSION >= (2, 11):
extra_kwargs["fallback"] = fallback extra_kwargs["fallback"] = fallback
else: if PYDANTIC_MINOR_VERSION >= (2, 12):
extra_kwargs = {} extra_kwargs["exclude_computed_fields"] = exclude_computed_fields
if IS_PYDANTIC_V2: if IS_PYDANTIC_V2:
return super().model_dump( return super().model_dump(
mode=mode, mode=mode,