-
Notifications
You must be signed in to change notification settings - Fork 253
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
ability to pass context to serialization (pydantic#7143) #1215
Conversation
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #1215 +/- ##
==========================================
- Coverage 90.21% 89.86% -0.36%
==========================================
Files 106 106
Lines 16339 16474 +135
Branches 36 40 +4
==========================================
+ Hits 14740 14804 +64
- Misses 1592 1651 +59
- Partials 7 19 +12
... and 6 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
|
CodSpeed Performance ReportMerging #1215 will not alter performanceComparing Summary
|
I think this is an artefact? Those tests only involve validation, and I only touched the serialization... or so I think. |
as for the pypy3.9 test failing, i honestly have too little experience with Rust to have a clue as to why |
please review |
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.
Thanks! Overall this looks great, clean implementation given first use of Rust! Hopefully you enjoyed the experience of working with Rust & PyO3 :)
I just have a few small thoughts which would nice to tidy up...
#[pyo3(get)] | ||
context: Option<PyObject>, |
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.
As context
is arbitrary user contents which might end up creating a circular reference, it's probably a good idea to add a __traverse__
implementation so that the Python GC can see into these fields. (Probably we should have already done this with include
/ exclude
, though with proper use circular references seem less likely for those.)
Would you be willing to help add this please?
https://pyo3.rs/v0.20.3/class/protocols#garbage-collector-integration
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.
latest commit should resolve this :)
This is sadly just PyPy flakiness which nobody can get to the bottom of, see it all over this place in PyO3/pyo3#3766 |
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.
Looks great, thanks very much!
awesome, thank you! i'm gonna submit a PR for pydantic now 👍 |
the PR is ready, i'll wait until the new pydantic-core release to submit it to pydantic @davidhewitt |
It might be worth opening the PR anyway, even if it doesn't build yet, so that we can remember this needs to go into pydantic 2.7. |
Change Summary
As described and discussed in pydantic#7143, it would make sense to be able to pass a
context
object to.model_dump()
/.model_dump_json()
in order to dynamically update the serialization behavior during runtime.This PR is my attempt at implementing this feature. This is my first time with Rust, so you should be wary about it :).
I tested this implementation with (an accordingly-modified) pydantic and everything seemed to work.
Related issue number
pydantic#7143
The pydantic side of the change is very light, I have a commit ready for it in case this PR gets merged.
Checklist
pydantic-core
(except for expected changes)Selected Reviewer: @adriangb