Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork782
Description
Privileged issue
- I'm@tiangolo or he asked me directly to create an issue here.
Issue Content
Currentlyalias parameter doesn't work as expected with Pydantic V2 installed.
Let's use this issue to track this.
👇 Here is a list of tests that showcase the problem (in the details):
fromtypingimportType,UnionimportpytestfrompydanticimportVERSION,BaseModel,ValidationErrorfrompydanticimportFieldasPFieldfromsqlmodelimportField,SQLModel# -----------------------------------------------------------------------------------# ModelsclassPydanticUser(BaseModel):full_name:str=PField(alias="fullName")classSQLModelUser(SQLModel):full_name:str=Field(alias="fullName")# Models with config (validate_by_name=True)ifVERSION.startswith("2."):classPydanticUserWithConfig(PydanticUser):model_config= {"validate_by_name":True}classSQLModelUserWithConfig(SQLModelUser):model_config= {"validate_by_name":True}else:classPydanticUserWithConfig(PydanticUser):classConfig:allow_population_by_field_name=TrueclassSQLModelUserWithConfig(SQLModelUser):classConfig:allow_population_by_field_name=True# -----------------------------------------------------------------------------------# Tests# Test validate by name@pytest.mark.parametrize("model", [PydanticUser,SQLModelUser])deftest_create_with_field_name(model:Union[Type[PydanticUser],Type[SQLModelUser]]):withpytest.raises(ValidationError):model(full_name="Alice")@pytest.mark.parametrize("model", [PydanticUserWithConfig,SQLModelUserWithConfig])deftest_create_with_field_name_with_config(model:Union[Type[PydanticUserWithConfig],Type[SQLModelUserWithConfig]],):user=model(full_name="Alice")assertuser.full_name=="Alice"# Test validate by alias@pytest.mark.parametrize("model", [PydanticUser,SQLModelUser,PydanticUserWithConfig,SQLModelUserWithConfig],)deftest_create_with_alias(model:Union[Type[PydanticUser],Type[SQLModelUser],Type[PydanticUserWithConfig],Type[SQLModelUserWithConfig], ],):user=model(fullName="Bob")# using aliasassertuser.full_name=="Bob"# Test validate by name and alias@pytest.mark.parametrize("model", [PydanticUserWithConfig,SQLModelUserWithConfig])deftest_create_with_both_prefers_alias(model:Union[Type[PydanticUserWithConfig],Type[SQLModelUserWithConfig]],):user=model(full_name="IGNORED",fullName="Charlie")assertuser.full_name=="Charlie"# alias should take precedence# Test serialize@pytest.mark.parametrize("model", [PydanticUser,SQLModelUser])deftest_dict_default_uses_field_names(model:Union[Type[PydanticUser],Type[SQLModelUser]],):user=model(fullName="Dana")data=user.dict()assert"full_name"indataassert"fullName"notindataassertdata["full_name"]=="Dana"# Test serialize by alias@pytest.mark.parametrize("model", [PydanticUser,SQLModelUser])deftest_dict_default_uses_aliases(model:Union[Type[PydanticUser],Type[SQLModelUser]],):user=model(fullName="Dana")data=user.dict(by_alias=True)assert"fullName"indataassert"full_name"notindataassertdata["fullName"]=="Dana"# Test json by alias@pytest.mark.parametrize("model", [PydanticUser,SQLModelUser])deftest_json_by_alias(model:Union[Type[PydanticUser],Type[SQLModelUser]],):user=model(fullName="Frank")json_data=user.json(by_alias=True)assert ('"fullName":"Frank"'injson_data)or ('"fullName": "Frank"'injson_data)assert"full_name"notinjson_data
When run with Pydantic V2:
t.py::test_create_with_field_name[PydanticUser] PASSEDt.py::test_create_with_field_name[SQLModelUser] FAILEDt.py::test_create_with_field_name_with_config[PydanticUserWithConfig] PASSEDt.py::test_create_with_field_name_with_config[SQLModelUserWithConfig] PASSEDt.py::test_create_with_alias[PydanticUser] PASSEDt.py::test_create_with_alias[SQLModelUser] FAILEDt.py::test_create_with_alias[PydanticUserWithConfig] PASSEDt.py::test_create_with_alias[SQLModelUserWithConfig] FAILEDt.py::test_create_with_both_prefers_alias[PydanticUserWithConfig] PASSEDt.py::test_create_with_both_prefers_alias[SQLModelUserWithConfig] FAILEDt.py::test_dict_default_uses_field_names[PydanticUser] PASSEDt.py::test_dict_default_uses_field_names[SQLModelUser] FAILEDt.py::test_dict_default_uses_aliases[PydanticUser] PASSEDt.py::test_dict_default_uses_aliases[SQLModelUser] FAILEDt.py::test_json_by_alias[PydanticUser] PASSEDt.py::test_json_by_alias[SQLModelUser] FAILEDAll tests parameterized with both, Pydantic model and SQLModel model, to show the difference in behavior.
With Pydantic V1 all tests pass.
With Pydantic V2 most of tests fail with SQLModel model.
I would also expect thatalias is used as a default value of column name. But it should be possible to override it by providingsa_column=Column("column_name") orsa_column_kwargs={"name": "column_name"}.
Any feedback is welcome. Please, review the set of tests, suggest use-cases that are currently not covered.
Would be nice to add set of tests forvalidation_alias andserialization_alias as well.