Skip to content
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

Respect config.alias_generator in pydantic dataclass #5673

Merged
merged 3 commits into from
May 12, 2023

Conversation

hramezani
Copy link
Member

@hramezani hramezani commented May 3, 2023

Fixes #4286

Selected Reviewer: @Kludex

@hramezani
Copy link
Member Author

please review

Comment on lines 538 to 544
validation_alias: str | list[str | int] | list[list[str | int]] | None = common_field['validation_alias']
serialization_alias: str | None = common_field['serialization_alias']
alias_generator = self.config_wrapper.alias_generator
if alias_generator:
validation_alias = alias_generator(name)
serialization_alias = alias_generator(name)

Copy link
Member Author

Choose a reason for hiding this comment

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

We can move this change into _common_field_schema but this function is used in _generate_td_field_schema as well. So, that's why I keep it in the _generate_dc_field_schema

Copy link
Contributor

@dmontagu dmontagu May 4, 2023

Choose a reason for hiding this comment

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

I think it may be a good idea to move into _common_field_schema since soon there will be something equivalent for _generate_model_field_schema (change coming down the pipe in pydantic core). Does it behave horribly/not work for typeddict if someone did add an alias? I think it's reasonable if someone might want to use camelCase aliases (specifically, via alias_generator) with typeddict too.

Copy link
Member Author

Choose a reason for hiding this comment

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

I moved it to _common_field_schema and also added a test for typeddict inspired by your example.

@dmontagu dmontagu assigned dmontagu and unassigned Kludex May 4, 2023
@hramezani hramezani force-pushed the dataclass_alias_generator branch 4 times, most recently from feca468 to fc481e4 Compare May 4, 2023 16:17
Copy link
Contributor

@dmontagu dmontagu left a comment

Choose a reason for hiding this comment

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

Actually there is a problem here related to overriding aliases. I think we need to respect the alias priority stuff.

@@ -263,7 +263,7 @@ print(Character.model_json_schema(by_alias=True))
{
'type': 'object',
'properties': {
'ActorName': {'type': 'string', 'default': None, 'title': 'Actorname'},
'Name': {'type': 'string', 'default': None, 'title': 'Name'},
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this attribute name should be changed, because the alias generator isn't supposed to override explicitly-set aliases.

The relevant attribute is FieldInfo.alias_priority.

I think this shouldn't be too hard to fix, but we should look at how that is used now.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we probably want to make a version of pydantic._internal._model_construction.apply_alias_generator for dataclasses / typeddict

@hramezani hramezani force-pushed the dataclass_alias_generator branch 2 times, most recently from 92a03d2 to 7be5b9e Compare May 5, 2023 08:39
@hramezani
Copy link
Member Author

please review

@dmontagu dmontagu assigned dmontagu and unassigned hramezani May 8, 2023
@hramezani hramezani merged commit 54a2560 into pydantic:main May 12, 2023
41 checks passed
@hramezani hramezani deleted the dataclass_alias_generator branch May 12, 2023 10:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Alias generator with Pydantic dataclass not behaving as expected
3 participants