-
-
Notifications
You must be signed in to change notification settings - Fork 782
🐛 Fix alias support for Pydantic v2
#1577
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
785270a
72e8c36
15c7954
3cc2b1e
34d2a45
6667109
382a52d
d3e761e
e3dbf75
e07a259
6b7a0a1
322c6af
6b13381
7b2bc2c
68b31d7
9440956
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -215,6 +215,8 @@ def Field( | |
| *, | ||
| default_factory: Optional[NoArgAnyCallable] = None, | ||
| alias: Optional[str] = None, | ||
| validation_alias: Optional[str] = None, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pydantic's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't have a strong preference on this. |
||
| serialization_alias: Optional[str] = None, | ||
| title: Optional[str] = None, | ||
| description: Optional[str] = None, | ||
| exclude: Union[ | ||
|
|
@@ -260,6 +262,8 @@ def Field( | |
| *, | ||
| default_factory: Optional[NoArgAnyCallable] = None, | ||
| alias: Optional[str] = None, | ||
| validation_alias: Optional[str] = None, | ||
| serialization_alias: Optional[str] = None, | ||
| title: Optional[str] = None, | ||
| description: Optional[str] = None, | ||
| exclude: Union[ | ||
|
|
@@ -314,6 +318,8 @@ def Field( | |
| *, | ||
| default_factory: Optional[NoArgAnyCallable] = None, | ||
| alias: Optional[str] = None, | ||
| validation_alias: Optional[str] = None, | ||
| serialization_alias: Optional[str] = None, | ||
| title: Optional[str] = None, | ||
| description: Optional[str] = None, | ||
| exclude: Union[ | ||
|
|
@@ -349,6 +355,8 @@ def Field( | |
| *, | ||
| default_factory: Optional[NoArgAnyCallable] = None, | ||
| alias: Optional[str] = None, | ||
| validation_alias: Optional[str] = None, | ||
| serialization_alias: Optional[str] = None, | ||
| title: Optional[str] = None, | ||
| description: Optional[str] = None, | ||
| exclude: Union[ | ||
|
|
@@ -387,43 +395,65 @@ def Field( | |
| schema_extra: Optional[Dict[str, Any]] = None, | ||
| ) -> Any: | ||
| current_schema_extra = schema_extra or {} | ||
| field_info = FieldInfo( | ||
| default, | ||
| default_factory=default_factory, | ||
| alias=alias, | ||
| title=title, | ||
| description=description, | ||
| exclude=exclude, | ||
| include=include, | ||
| const=const, | ||
| gt=gt, | ||
| ge=ge, | ||
| lt=lt, | ||
| le=le, | ||
| multiple_of=multiple_of, | ||
| max_digits=max_digits, | ||
| decimal_places=decimal_places, | ||
| min_items=min_items, | ||
| max_items=max_items, | ||
| unique_items=unique_items, | ||
| min_length=min_length, | ||
| max_length=max_length, | ||
| allow_mutation=allow_mutation, | ||
| regex=regex, | ||
| discriminator=discriminator, | ||
| repr=repr, | ||
| primary_key=primary_key, | ||
| foreign_key=foreign_key, | ||
| ondelete=ondelete, | ||
| unique=unique, | ||
| nullable=nullable, | ||
| index=index, | ||
| sa_type=sa_type, | ||
| sa_column=sa_column, | ||
| sa_column_args=sa_column_args, | ||
| sa_column_kwargs=sa_column_kwargs, | ||
| field_info_kwargs = { | ||
| "alias": alias, | ||
| "title": title, | ||
| "description": description, | ||
| "exclude": exclude, | ||
| "include": include, | ||
| "const": const, | ||
| "gt": gt, | ||
| "ge": ge, | ||
| "lt": lt, | ||
| "le": le, | ||
| "multiple_of": multiple_of, | ||
| "max_digits": max_digits, | ||
| "decimal_places": decimal_places, | ||
| "min_items": min_items, | ||
| "max_items": max_items, | ||
| "unique_items": unique_items, | ||
| "min_length": min_length, | ||
| "max_length": max_length, | ||
| "allow_mutation": allow_mutation, | ||
| "regex": regex, | ||
| "discriminator": discriminator, | ||
| "repr": repr, | ||
| "primary_key": primary_key, | ||
| "foreign_key": foreign_key, | ||
| "ondelete": ondelete, | ||
| "unique": unique, | ||
| "nullable": nullable, | ||
| "index": index, | ||
| "sa_type": sa_type, | ||
| "sa_column": sa_column, | ||
| "sa_column_args": sa_column_args, | ||
| "sa_column_kwargs": sa_column_kwargs, | ||
| **current_schema_extra, | ||
| ) | ||
| } | ||
| if IS_PYDANTIC_V2: | ||
| # Add Pydantic v2 specific parameters | ||
| field_info_kwargs.update( | ||
| { | ||
| "validation_alias": validation_alias, | ||
| "serialization_alias": serialization_alias, | ||
| } | ||
| ) | ||
| field_info = FieldInfo( | ||
| default, | ||
| default_factory=default_factory, | ||
| **field_info_kwargs, | ||
| ) | ||
YuriiMotov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| else: | ||
| if validation_alias: | ||
| raise RuntimeError("validation_alias is not supported in Pydantic v1") | ||
| if serialization_alias: | ||
| raise RuntimeError("serialization_alias is not supported in Pydantic v1") | ||
| field_info = FieldInfo( | ||
| default, | ||
| default_factory=default_factory, | ||
| **field_info_kwargs, | ||
| ) | ||
|
|
||
| post_init_field_info(field_info) | ||
| return field_info | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| from typing import Type, Union | ||
|
|
||
| import pytest | ||
| from pydantic import VERSION, BaseModel, ValidationError | ||
| from pydantic import Field as PField | ||
| from sqlmodel import Field, SQLModel | ||
|
|
||
| # ----------------------------------------------------------------------------------- | ||
| # Models | ||
YuriiMotov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| class PydanticUser(BaseModel): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should think whether we want to keep parameterizing tests with Pydantic models. If it's useful to keep this parametrization in repo? 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally, I think it’s useful to keep the parametrization for now, since it acts as a kind of integration test between the two libraries. |
||
| full_name: str = PField(alias="fullName") | ||
|
|
||
|
|
||
| class SQLModelUser(SQLModel): | ||
| full_name: str = Field(alias="fullName") | ||
|
|
||
|
|
||
| # Models with config (validate_by_name=True) | ||
|
|
||
|
|
||
| if VERSION.startswith("2."): | ||
|
|
||
| class PydanticUserWithConfig(PydanticUser): | ||
| model_config = {"validate_by_name": True} | ||
|
|
||
| class SQLModelUserWithConfig(SQLModelUser): | ||
| model_config = {"validate_by_name": True} | ||
|
|
||
| else: | ||
|
|
||
| class PydanticUserWithConfig(PydanticUser): | ||
| class Config: | ||
| allow_population_by_field_name = True | ||
|
|
||
| class SQLModelUserWithConfig(SQLModelUser): | ||
| class Config: | ||
| allow_population_by_field_name = True | ||
|
|
||
|
|
||
| # ----------------------------------------------------------------------------------- | ||
| # Tests | ||
|
|
||
| # Test validate by name | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("model", [PydanticUser, SQLModelUser]) | ||
| def test_create_with_field_name(model: Union[Type[PydanticUser], Type[SQLModelUser]]): | ||
| with pytest.raises(ValidationError): | ||
| model(full_name="Alice") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("model", [PydanticUserWithConfig, SQLModelUserWithConfig]) | ||
| def test_create_with_field_name_with_config( | ||
| model: Union[Type[PydanticUserWithConfig], Type[SQLModelUserWithConfig]], | ||
| ): | ||
| user = model(full_name="Alice") | ||
| assert user.full_name == "Alice" | ||
|
|
||
|
|
||
| # Test validate by alias | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "model", | ||
| [PydanticUser, SQLModelUser, PydanticUserWithConfig, SQLModelUserWithConfig], | ||
| ) | ||
| def test_create_with_alias( | ||
| model: Union[ | ||
| Type[PydanticUser], | ||
| Type[SQLModelUser], | ||
| Type[PydanticUserWithConfig], | ||
| Type[SQLModelUserWithConfig], | ||
| ], | ||
| ): | ||
| user = model(fullName="Bob") # using alias | ||
| assert user.full_name == "Bob" | ||
|
|
||
|
|
||
| # Test validate by name and alias | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("model", [PydanticUserWithConfig, SQLModelUserWithConfig]) | ||
| def test_create_with_both_prefers_alias( | ||
| model: Union[Type[PydanticUserWithConfig], Type[SQLModelUserWithConfig]], | ||
| ): | ||
| user = model(full_name="IGNORED", fullName="Charlie") | ||
| assert user.full_name == "Charlie" # alias should take precedence | ||
|
|
||
|
|
||
| # Test serialize | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("model", [PydanticUser, SQLModelUser]) | ||
| def test_dict_default_uses_field_names( | ||
| model: Union[Type[PydanticUser], Type[SQLModelUser]], | ||
| ): | ||
| user = model(fullName="Dana") | ||
| data = user.dict() | ||
| assert "full_name" in data | ||
| assert "fullName" not in data | ||
| assert data["full_name"] == "Dana" | ||
|
|
||
|
|
||
| # Test serialize by alias | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("model", [PydanticUser, SQLModelUser]) | ||
| def test_dict_default_uses_aliases( | ||
| model: Union[Type[PydanticUser], Type[SQLModelUser]], | ||
| ): | ||
| user = model(fullName="Dana") | ||
| data = user.dict(by_alias=True) | ||
| assert "fullName" in data | ||
| assert "full_name" not in data | ||
| assert data["fullName"] == "Dana" | ||
|
|
||
|
|
||
| # Test json by alias | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("model", [PydanticUser, SQLModelUser]) | ||
| def test_json_by_alias( | ||
| model: Union[Type[PydanticUser], Type[SQLModelUser]], | ||
| ): | ||
| user = model(fullName="Frank") | ||
| json_data = user.json(by_alias=True) | ||
| assert ('"fullName":"Frank"' in json_data) or ('"fullName": "Frank"' in json_data) | ||
| assert "full_name" not in json_data | ||
|
|
||
|
|
||
| # Pydantic v2 specific models - only define if we're running Pydantic v2 | ||
| if VERSION.startswith("2."): | ||
|
|
||
| class PydanticUserV2(BaseModel): | ||
| first_name: str = PField( | ||
| validation_alias="firstName", serialization_alias="f_name" | ||
| ) | ||
|
|
||
| class SQLModelUserV2(SQLModel): | ||
| first_name: str = Field( | ||
| validation_alias="firstName", serialization_alias="f_name" | ||
| ) | ||
| else: | ||
| # Dummy classes for Pydantic v1 to prevent import errors | ||
| PydanticUserV2 = None | ||
| SQLModelUserV2 = None | ||
YuriiMotov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @pytest.mark.skipif( | ||
| not VERSION.startswith("2."), | ||
| reason="validation_alias and serialization_alias are not supported in Pydantic v1", | ||
| ) | ||
YuriiMotov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @pytest.mark.parametrize("model", [PydanticUserV2, SQLModelUserV2]) | ||
| def test_create_with_validation_alias( | ||
| model: Union[Type[PydanticUserV2], Type[SQLModelUserV2]], | ||
| ): | ||
| user = model(firstName="John") | ||
| assert user.first_name == "John" | ||
|
|
||
|
|
||
| @pytest.mark.skipif( | ||
| not VERSION.startswith("2."), | ||
| reason="validation_alias and serialization_alias are not supported in Pydantic v1", | ||
| ) | ||
| @pytest.mark.parametrize("model", [PydanticUserV2, SQLModelUserV2]) | ||
| def test_serialize_with_serialization_alias( | ||
| model: Union[Type[PydanticUserV2], Type[SQLModelUserV2]], | ||
| ): | ||
| user = model(firstName="Jane") | ||
| data = user.dict(by_alias=True) | ||
| assert "f_name" in data | ||
| assert "firstName" not in data | ||
| assert "first_name" not in data | ||
| assert data["f_name"] == "Jane" | ||
Uh oh!
There was an error while loading. Please reload this page.