-
Notifications
You must be signed in to change notification settings - Fork 6
Enhance Python Object Deserialization via __dict__ Field #22
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
Enhance Python Object Deserialization via __dict__ Field #22
Conversation
Waiting for Jij-Inc/serde-pyobject#22 to be merged
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. |
Thanks! I merged your PR. |
@termoshtt Hello, could you please review this? |
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.
Great idea! I will merge with minor fixes.
Merged via #27, Thanks! |
Awesome, thanks for merging this. |
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_pyobject
should 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.