| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
This commit fixes Field(alias="...") functionality in Pydantic v2 while maintaining full backward compatibility with Pydantic v1.
There was a problem hiding this comment.
The idea looks good to me!
A couple of more things:
PS: @svlandeg, sorry, I only noticed you self-assigned this PR when I was about to press "Submit review".. Hope you don't mind I submitted this review)
Sorry, something went wrong.
| *, | ||
| default_factory: Optional[NoArgAnyCallable] = None, | ||
| alias: Optional[str] = None, | ||
| validation_alias: Optional[str] = None, |
There was a problem hiding this comment.
Pydantic's validation_alias type annotation is wider (validation_alias: str | AliasPath | AliasChoices | None), but I think it's fine if we only support str for now and extend it later
Sorry, something went wrong.
There was a problem hiding this comment.
I don't have a strong preference on this.
Sorry, something went wrong.
Co-authored-by: Motov Yurii <109919500+YuriiMotov@users.noreply.github.com>
Of course not! I tentatively assign PRs when I want to review them in the near future, just to keep track of them, but sometimes other tasks come in between 🙈 Feel free to review whatever you want! |
Sorry, something went wrong.
|
@YuriiMotov @svlandeg I’ve addressed all the comments from the review. Let me know if there’s anything else you’d like me to focus on. |
Sorry, something went wrong.
Co-authored-by: Motov Yurii <109919500+YuriiMotov@users.noreply.github.com>
|
I simplified code a bit - moved alias propagation logic to Field.__init__ so that it would be all in one place. |
Sorry, something went wrong.
There was a problem hiding this comment.
LGTM!
Implementation is not breaking. It also handles the workaround with
Field(schema_extra={"validation_alias": "field_alias"})
that people currently use.
The only use case it can break is if users use
Field(schema_extra={"validation_alias": "field_alias"})
and want to use this model with both, Pydantic V1 and V2.
It will now raise
RuntimeError("validation_alias is not supported in Pydantic v1")
for Pydantic V1.
But it's easily fixable - just remove validation_alias (now alias works with both versions, so that workaround is not needed).
Some tests are parameterized to be run with both, SQLModel and Pydantic models, to show that they behave the same way. If you don't like it, it's easy to remove it.
@ravishan16, thank you!
Sorry, something went wrong.
|
Could you please let me know when this PR will be merged and released? |
Sorry, something went wrong.
There was a problem hiding this comment.
Awesome, thank you @ravishan16! 🚀
And thanks @YuriiMotov! 🍰
This will be available in SQLModel 0.0.29, released in the next hours. 🎉
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Description
This PR fixes the Field(alias="...") functionality that was broken in Pydantic v2 while maintaining full backward compatibility with Pydantic v1.
Problem
In Pydantic v2, the alias parameter in SQLModel's Field function was not working correctly for validation and serialization. The issue was that Pydantic v2 requires explicit validation_alias and serialization_alias parameters, but SQLModel wasn't automatically propagating the alias value to these parameters.
Solution
Changes Made:
Enhanced Field function:
Automatic alias propagation:
Backward compatibility:
Comprehensive testing:
Breaking Changes
None. This is purely a bug fix with backward compatibility maintained.
Closes
Fixes #1536