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

fix: support pydantic.Field(kw_only=True) with inherited dataclasses #7827

Merged
merged 3 commits into from Oct 20, 2023

Conversation

PrettyWood
Copy link
Member

@PrettyWood PrettyWood commented Oct 14, 2023

Change Summary

pydantic dataclasses support pydantic.Field and also support kw_only parameter.
But when we set pydantic.Field(kw_only=True) as value of a field, the stdlib dataclass behind the scene would use a field without the option causing some differences with the usage of pure stdlib dataclasses.

Related issue number

closes #7770

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Selected Reviewer: @hramezani

@PrettyWood PrettyWood added the relnotes-fix Used for bugfixes. label Oct 14, 2023
except AttributeError:
# no default value has been set for this field
continue
if isinstance(field_value, FieldInfo) and field_value.kw_only:
Copy link
Member Author

Choose a reason for hiding this comment

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

For now I went with the smallest change possible instead of wrapping them all the time and forward other stuff like init. I suggest we wait to see if this method needs to more than that

@PrettyWood
Copy link
Member Author

please review

pydantic/dataclasses.py Outdated Show resolved Hide resolved
@hramezani
Copy link
Member

Thanks @PrettyWood for this patch 🙏

It looks good to me 👍

Let's see @dmontagu opinion here.

@hramezani hramezani assigned dmontagu and unassigned hramezani Oct 16, 2023
Co-authored-by: Hasan Ramezani <hasan.r67@gmail.com>
Copy link
Member

@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

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

LGTM, and @dmontagu said he approved of the changes as well.

@PrettyWood as you mentioned here, I think it's worth us revisiting this new functionality and seeing if we should account for other things like init, etc.

@sydney-runkle sydney-runkle merged commit 8b88a9e into pydantic:main Oct 20, 2023
59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

kw_only=True does not work in inheritance for pydantic.Field
4 participants