Enhance Python Object Deserialization via __dict__ Field#22
Merged
termoshtt merged 4 commits intoJij-Inc:mainfrom May 24, 2025
Merged
Enhance Python Object Deserialization via __dict__ Field#22termoshtt merged 4 commits intoJij-Inc:mainfrom
termoshtt merged 4 commits intoJij-Inc:mainfrom
Conversation
Contributor
Author
LockedThread
added a commit
to LockedThread/pyo3-pylogger
that referenced
this pull request
Apr 2, 2025
Waiting for Jij-Inc/serde-pyobject#22 to be merged
Contributor
|
Hey, thank you so both so much for this library and this PR! I've added a fix to support objects with a It would be great if that could be included too if this gets merged. |
Contributor
Author
Thanks! I merged your PR. |
Contributor
Author
|
@termoshtt Hello, could you please review this? |
termoshtt
approved these changes
May 24, 2025
Member
termoshtt
left a comment
There was a problem hiding this comment.
Great idea! I will merge with minor fixes.
Merged
Member
|
Merged via #27, Thanks! |
Contributor
Author
Awesome, thanks for merging this. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR introduces an enhancement, allowing seamless deserialization of Python objects—specifically handling instances of Python classes.
Overview
New Deserializer Check:
The updated deserializer now includes a check to determine if the object being deserialized is an instance of a Python class. If it is, the implementation extracts the object's
__dict__field, which contains the object's attributes as a dictionary. This dictionary is then used as the source for deserialization, allowing for a more natural mapping between Python objects and Rust data structures.Improved Compatibility:
With this enhancement, the wrapper now better supports the dynamic nature of Python objects. Users can now serialize and deserialize custom Python class instances without needing to implement extra boilerplate code to manually handle the conversion of attributes.
Motivation
The primary motivation behind this change was to simplify the interoperability between Python and Rust for users who leverage Serde for serialization tasks. Python classes typically encapsulate their state within the
__dict__attribute, and by automatically using it during deserialization, we:Reduce Friction:
Developers no longer need to write custom deserialization logic for Python class instances.
Increase Reliability:
The wrapper can more reliably reconstruct Python objects in Rust by leveraging the built-in dictionary representation.
Testing
Added new unit tests covering:
__dict__.Future Work
Enhanced Error Reporting:
Implementing [Improvements]
from_pyobjectshould not panic on unknown types #12Support for More Pydantic BaseModel:
Evaluate the possibility of extending similar logic to handle other Python-specific constructs like Pydantic BaseModels, which we can use the model_dump function instead.