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

Support Field in dataclass + 'metadata' kwarg of dataclasses.field #2384

Merged
merged 4 commits into from Feb 25, 2021

Conversation

PrettyWood
Copy link
Member

@PrettyWood PrettyWood commented Feb 22, 2021

Change Summary

When we use pydantic dataclass, we are limited by current dataclasses.field to declare a field.
Now we can either use Field like for a BaseModel or leverage dataclasses.field by using the 'metadata' kwarg

Related issue number

closes #470
closes #2382

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • changes/<pull request or issue id>-<github username>.md file added describing change
    (see changes/README.md for details)

…ield`

Please enter the commit message for your changes. Lines starting
@codecov
Copy link

codecov bot commented Feb 22, 2021

Codecov Report

Merging #2384 (a5bcea9) into master (c8883e3) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #2384   +/-   ##
=======================================
  Coverage   99.90%   99.90%           
=======================================
  Files          25       25           
  Lines        5039     5051   +12     
  Branches     1031     1034    +3     
=======================================
+ Hits         5034     5046   +12     
  Misses          1        1           
  Partials        4        4           
Impacted Files Coverage Δ
pydantic/dataclasses.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c8883e3...a5bcea9. Read the comment docs.

default=None,
metadata=dict(title='The age of the user', description='do not lie!')
)
height: Optional[int] = Field(None, title='The height in cm', ge=50, le=300)
Copy link
Member

Choose a reason for hiding this comment

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

😄 👀

Copy link
Member Author

Choose a reason for hiding this comment

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

wink wink


# We need to remove `FieldInfo` values since they are not valid as input
# It's ok since they are obviously the default values
input_data = {k: v for k, v in self.__dict__.items() if not isinstance(v, FieldInfo)}
Copy link
Member

Choose a reason for hiding this comment

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

Humm, I wonder if this will have an adverse effect on performance?

Perhaps we could add a flag somehow below when any fields use Field, then only run this logic if so?

Copy link
Member Author

Choose a reason for hiding this comment

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

relevant commit fb97e08

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.

Use metadata in dataclass field to populate pydantic Field Support "Field()" on dataclasses
2 participants