Skip to content

Fix pydantic_model_creator incompatibility with Pydantic 2.11 (#1925) #1930

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

Merged
merged 1 commit into from
Apr 7, 2025

Conversation

C5H12O5
Copy link
Contributor

@C5H12O5 C5H12O5 commented Apr 7, 2025

Description

Modified the parameters passed to Pydantic's create_model method to ensure compatibility with Pydantic 2.11.

Motivation and Context

Fixes #1925

How Has This Been Tested?

make test

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

base_model = type(
"BasePydanticModel",
(PydanticModel,),
{"model_config": self._pconfig, **computed_fields},
Copy link

Choose a reason for hiding this comment

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

Note that you can specify config in create_model() via the __config__ parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review. I did notice this parameter while checking the source code, but it seems __config__ cannot be used together with __base__.

Copy link

Choose a reason for hiding this comment

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

Hum, this limitation was introduced a really long time ago, when create_model() was added in pydantic/pydantic#125. I'm pretty sure this limitation can be removed now; it is possible to specify model_config on a subclass inheriting from (multiple) bases. This will be tracked in pydantic/pydantic#2137.

Choose a reason for hiding this comment

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

@Viicos pydantic I assume not accepting model_config as field parameter to create_model is a regression introduced in 2.11.0?

Copy link

Choose a reason for hiding this comment

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

If it was working before 2.11, this was by accident. The proper and documented way was (and still is) to provide configuration using the __config__ parameter.

Copy link

codspeed-hq bot commented Apr 7, 2025

CodSpeed Performance Report

Merging #1930 will not alter performance

Comparing C5H12O5:fix-pydantic-model-creator (cf61111) with develop (21463c7)

Summary

✅ 16 untouched benchmarks

@coveralls
Copy link

Pull Request Test Coverage Report for Build 14303714936

Details

  • 10 of 10 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.01%) to 89.671%

Totals Coverage Status
Change from base Build 14102340932: 0.01%
Covered Lines: 6668
Relevant Lines: 7256

💛 - Coveralls

@henadzit henadzit requested a review from Copilot April 7, 2025 22:03
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 2 out of 3 changed files in this pull request and generated no comments.

Files not reviewed (1)
  • CHANGELOG.rst: Language not supported
Comments suppressed due to low confidence (2)

tortoise/contrib/pydantic/creator.py:391

  • [nitpick] Consider using hasattr(v, 'decorator_info') to check for the presence of a computed field attribute, which may more clearly express your intent.
if isinstance(getattr(v, "decorator_info", None), ComputedFieldInfo):

tortoise/contrib/pydantic/creator.py:402

  • Verify that all necessary properties from self._properties are preserved when splitting into common_fields and computed_fields. Ensure that the base_model fully replicates the configuration required by downstream usage.
__base__=base_model,

Copy link
Contributor

@henadzit henadzit left a comment

Choose a reason for hiding this comment

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

The change looks good to me, thank you for your contribution!

@henadzit henadzit merged commit 07a4f25 into tortoise:develop Apr 7, 2025
9 checks passed
@commonism
Copy link

Hi,

I'm with you.
I figured not adding an annotation if the annotation for the field is None works as well.
pydantic/pydantic#11032 (comment)
Therefore my fix is duplicating create_model.
commonism/aiopenapi3@8916b46

This was referenced Apr 13, 2025
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.

Can't do pydantic serialization
5 participants