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 upcoming changes in Pydantic version 2.10 #1215

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mtr
Copy link

@mtr mtr commented Nov 15, 2024

Without this fix, any SQLModel-based model with table=True and a Field that sets default_factory=... will brake with the following error message: ValueError: 'validated_data' must be provided if 'call_default_factory' is True.

To reproduce, before this fix is applied:

  • Install all project dependencies, then upgrade Pydantic:

    pip install -r requirements.txt
    pip install pydantic==2.10.0b2
  • Run the test suite:

    python -m pytest tests

… which will give the following output:

[…]
=================================================================== short test summary info ====================================================================
FAILED tests/test_annotated_uuid.py::test_annotated_optional_types - ValueError: 'validated_data' must be provided if 'call_default_factory' is True.
FAILED tests/test_advanced/test_uuid/test_tutorial001.py::test_tutorial - ValueError: 'validated_data' must be provided if 'call_default_factory' is True.
FAILED tests/test_advanced/test_uuid/test_tutorial001_py310.py::test_tutorial - ValueError: 'validated_data' must be provided if 'call_default_factory' is True.
FAILED tests/test_advanced/test_uuid/test_tutorial002.py::test_tutorial - ValueError: 'validated_data' must be provided if 'call_default_factory' is True.
FAILED tests/test_advanced/test_uuid/test_tutorial002_py310.py::test_tutorial - ValueError: 'validated_data' must be provided if 'call_default_factory' is True.
===================================================== 5 failed, 250 passed, 3 skipped, 7 warnings in 9.37s =====================================================

mtr added 2 commits November 15, 2024 10:39
Without this fix, any SQLModel-based model with `table=True` and a `Field` that sets `default_factory=...` will brake with the following error message: `ValueError: 'validated_data' must be provided if 'call_default_factory' is True.`
Without this fix, any SQLModels with a `Field` that uses `default_factory=...` will brake with the following error message: `ValueError: 'validated_data' must be provided if 'call_default_factory' is True`.

Made the fix backwards compatible.
@Viicos
Copy link

Viicos commented Nov 26, 2024

Note that we added a quick fix in 2.10.1 to not raise the error. But indeed, SQLModel should support default factories taking a validated_data argument.

The BaseModel.model_construct implementation was updated to take into account this feature, so it might be good looking at how we did it. Might be good looking at the other fixes that were introduced since then (e.g. pydantic/pydantic#9223).

(I'll also note that having SQModel relying so much on Pydantic is a bit scary, but well..)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants