-
Notifications
You must be signed in to change notification settings - Fork 252
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 exclude_if callable at field level #1535
base: main
Are you sure you want to change the base?
Support exclude_if callable at field level #1535
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1535 +/- ##
==========================================
- Coverage 90.21% 89.63% -0.58%
==========================================
Files 106 112 +6
Lines 16339 17908 +1569
Branches 36 40 +4
==========================================
+ Hits 14740 16052 +1312
- Misses 1592 1836 +244
- Partials 7 20 +13
... and 54 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
|
please review |
CodSpeed Performance ReportMerging #1535 will not alter performanceComparing Summary
|
src/serializers/fields.rs
Outdated
serializer: &CombinedSerializer, | ||
) -> PyResult<bool> { | ||
let py = value.py(); | ||
|
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.
think this should be called only if any condition is met, right? inside the if
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 think we should probably handle this outside of the exclude_default
function as to decouple them for readability. We don't couple exclude_default
with exclude_none
or exclude_unset
, so I think we should retain separation here as well!
Super exciting! Will give this a thorough review soon. Working on getting v2.10 out, and this will be included in v2.11 :) |
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.
Would love to see an implementation on the pydantic
side as well - you can point to this branch.
Looks like a great start. I'm impressed with how simple this is going to be!
src/serializers/fields.rs
Outdated
serializer: &CombinedSerializer, | ||
) -> PyResult<bool> { | ||
let py = value.py(); | ||
|
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 think we should probably handle this outside of the exclude_default
function as to decouple them for readability. We don't couple exclude_default
with exclude_none
or exclude_unset
, so I think we should retain separation here as well!
I would anticipate that folks might want to set this at the |
Hello @sydney-runkle ! How do you do the implementation in |
I'm curious how the callable should work in the class Model(BaseModel):
foo: str
bar: list[str]
foobaz: int
>>> Model(foo = "", bar = [], foobaz = 1).model_dump(exclude_if = lambda field: not field)
{"foobaz": 1} |
Yes, exactly this! Once we merge, we'll include in a minor It's just helpful to see the implementation on both sides before we merge the |
Yes, exactly this. That being said, I chatted with the team, and we decided that introducing this at the field level first makes sense, and we can follow with other config / runtime specs later. |
d7d7d0e
to
b225197
Compare
Alright, @sydney-runkle I created a PR pointing to this branch! |
Change Summary
This PR aims to add support for excluding field from serialization based on condition. As @davidhewitt suggested I added a new
exclude_if
argument at field level, which is callable that checks if the field value meets a condition. However, in this pydantic issue David suggested to useskip_serializing_if
from Rust's serde, I could't find a way to use it due that serialization is being doing a loop for each field usingserialize_map
.Related issue number
pydantic/pydantic#10728
Checklist
pydantic-core
(except for expected changes)Selected Reviewer: @sydney-runkle