Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

👽️ Support Pydantic version 2.10 #1215

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion sqlmodel/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def partial_init() -> Generator[None, None, None]:


if IS_PYDANTIC_V2:
import inspect

from annotated_types import MaxLen
from pydantic import ConfigDict as BaseConfig
from pydantic._internal._fields import PydanticMetadata
Expand All @@ -80,6 +82,10 @@ def partial_init() -> Generator[None, None, None]:
from pydantic_core import PydanticUndefined as Undefined
from pydantic_core import PydanticUndefinedType as UndefinedType

PYDANCTIC_FIELD_GET_DEFAULT_REQUIRES_VALIDATED_DATA = (
"validated_data" in inspect.signature(FieldInfo.get_default).parameters
)
Comment on lines +85 to +87
Copy link

@Viicos Viicos Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't a version check be done instead? I think it would clearer with a comment:

Suggested change
PYDANCTIC_FIELD_GET_DEFAULT_REQUIRES_VALIDATED_DATA = (
"validated_data" in inspect.signature(FieldInfo.get_default).parameters
)
# Pydantic 2.10 added support for default factories taking validated data:
PYDANCTIC_FIELD_GET_DEFAULT_REQUIRES_VALIDATED_DATA = IS_PYDANTIC_210


# Dummy for types, to make it importable
class ModelField:
pass
Expand Down Expand Up @@ -252,7 +258,13 @@ def sqlmodel_table_construct(
elif name in values:
fields_values[name] = values.pop(name)
elif not field.is_required():
defaults[name] = field.get_default(call_default_factory=True)
defaults[name] = (
field.get_default(
call_default_factory=True, validated_data=fields_values
)
if PYDANCTIC_FIELD_GET_DEFAULT_REQUIRES_VALIDATED_DATA
else field.get_default(call_default_factory=True)
)
if _fields_set is None:
_fields_set = set(fields_values.keys())
fields_values.update(defaults)
Expand Down
Loading