-
-
Notifications
You must be signed in to change notification settings - Fork 768
🐛 Fix Field alias support for Pydantic v2 with backward compatibility #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 all commits
785270a
72e8c36
15c7954
3cc2b1e
34d2a45
6667109
382a52d
d3e761e
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,60 @@ def Field( | |
schema_extra: Optional[Dict[str, Any]] = None, | ||
) -> Any: | ||
current_schema_extra = schema_extra or {} | ||
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, | ||
} | ||
) | ||
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, | ||
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, | ||
**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,178 @@ | ||||||||||||
from typing import Type, Union | ||||||||||||
|
||||||||||||
import pytest | ||||||||||||
from pydantic import VERSION, BaseModel, ValidationError | ||||||||||||
from pydantic import Field as PField | ||||||||||||
from sqlmodel import Field, SQLModel | ||||||||||||
|
||||||||||||
from tests.conftest import needs_pydanticv2 | ||||||||||||
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.
Suggested change
|
||||||||||||
|
||||||||||||
""" | ||||||||||||
Alias tests for SQLModel and Pydantic compatibility | ||||||||||||
""" | ||||||||||||
|
||||||||||||
|
||||||||||||
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 | ||||||||||||
|
||||||||||||
|
||||||||||||
@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" | ||||||||||||
|
||||||||||||
|
||||||||||||
@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" | ||||||||||||
|
||||||||||||
|
||||||||||||
@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 | ||||||||||||
|
||||||||||||
|
||||||||||||
@pytest.mark.parametrize("model", [PydanticUser, SQLModelUser]) | ||||||||||||
def test_dict_default_uses_field_names( | ||||||||||||
model: Union[Type[PydanticUser], Type[SQLModelUser]], | ||||||||||||
): | ||||||||||||
user = model(fullName="Dana") | ||||||||||||
if VERSION.startswith("2."): | ||||||||||||
data = user.model_dump() | ||||||||||||
else: | ||||||||||||
data = user.dict() | ||||||||||||
assert "full_name" in data | ||||||||||||
assert "fullName" not in data | ||||||||||||
assert data["full_name"] == "Dana" | ||||||||||||
|
||||||||||||
|
||||||||||||
@pytest.mark.parametrize("model", [PydanticUser, SQLModelUser]) | ||||||||||||
def test_dict_default_uses_aliases( | ||||||||||||
model: Union[Type[PydanticUser], Type[SQLModelUser]], | ||||||||||||
): | ||||||||||||
user = model(fullName="Dana") | ||||||||||||
if VERSION.startswith("2."): | ||||||||||||
data = user.model_dump(by_alias=True) | ||||||||||||
else: | ||||||||||||
data = user.dict(by_alias=True) | ||||||||||||
assert "fullName" in data | ||||||||||||
assert "full_name" not in data | ||||||||||||
assert data["fullName"] == "Dana" | ||||||||||||
|
||||||||||||
|
||||||||||||
@pytest.mark.parametrize("model", [PydanticUser, SQLModelUser]) | ||||||||||||
def test_json_by_alias( | ||||||||||||
model: Union[Type[PydanticUser], Type[SQLModelUser]], | ||||||||||||
): | ||||||||||||
user = model(fullName="Frank") | ||||||||||||
if VERSION.startswith("2."): | ||||||||||||
json_data = user.model_dump_json(by_alias=True) | ||||||||||||
else: | ||||||||||||
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 | ||||||||||||
|
||||||||||||
|
||||||||||||
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
|
||||||||||||
|
||||||||||||
|
||||||||||||
def test_validation_alias_runtimeerror_pydantic_v1(): | ||||||||||||
if VERSION.startswith("2."): | ||||||||||||
pytest.skip("Only relevant for Pydantic v1") | ||||||||||||
Comment on lines
+141
to
+143
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.
Suggested change
|
||||||||||||
with pytest.raises( | ||||||||||||
RuntimeError, match="validation_alias is not supported in Pydantic v1" | ||||||||||||
): | ||||||||||||
Field(validation_alias="foo") | ||||||||||||
|
||||||||||||
|
||||||||||||
def test_serialization_alias_runtimeerror_pydantic_v1(): | ||||||||||||
if VERSION.startswith("2."): | ||||||||||||
pytest.skip("Only relevant for Pydantic v1") | ||||||||||||
Comment on lines
+150
to
+152
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.
Suggested change
|
||||||||||||
with pytest.raises( | ||||||||||||
RuntimeError, match="serialization_alias is not supported in Pydantic v1" | ||||||||||||
): | ||||||||||||
Field(serialization_alias="bar") | ||||||||||||
|
||||||||||||
|
||||||||||||
@needs_pydanticv2 | ||||||||||||
@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" | ||||||||||||
|
||||||||||||
|
||||||||||||
@needs_pydanticv2 | ||||||||||||
@pytest.mark.parametrize("model", [PydanticUserV2, SQLModelUserV2]) | ||||||||||||
def test_serialize_with_serialization_alias( | ||||||||||||
model: Union[Type[PydanticUserV2], Type[SQLModelUserV2]], | ||||||||||||
): | ||||||||||||
user = model(firstName="Jane") | ||||||||||||
data = user.model_dump(by_alias=True) | ||||||||||||
assert "f_name" in data | ||||||||||||
assert "firstName" not in data | ||||||||||||
assert "first_name" not in data | ||||||||||||
assert data["f_name"] == "Jane" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is declared inside the
if IS_PYDANTIC_V2:
block. So, this condition looks not necessary here.Am I missing something?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added for backward compatibility with Pydantic v1.
Without this, Pydantic tests v1 would fail.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just checked it with Python 3.11 and Pydantic 1.10.22 - tests passed.
Could you please check it one more time?