Skip to content

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

Merged
merged 4 commits into from
May 24, 2025

Conversation

LockedThread
Copy link
Contributor

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

  • Unit Tests:
    Added new unit tests covering:
    • Standard Python objects.
    • Custom Python class instances with populated __dict__.

Future Work

@LockedThread
Copy link
Contributor Author

Please review this before #23. #23 is a dependent PR.

@jobafr
Copy link
Contributor

jobafr commented May 16, 2025

Hey, thank you so both so much for this library and this PR! I've added a fix to support objects with a __slots__ attribute in addition to __dict__ based objects:

It would be great if that could be included too if this gets merged.

@LockedThread
Copy link
Contributor Author

Hey, thank you so both so much for this library and this PR! I've added a fix to support objects with a __slots__ attribute in addition to __dict__ based objects:

It would be great if that could be included too if this gets merged.

Thanks! I merged your PR.

@LockedThread
Copy link
Contributor Author

@termoshtt Hello, could you please review this?

@termoshtt termoshtt self-requested a review May 21, 2025 11:18
Copy link
Member

@termoshtt termoshtt left a 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.

@Copilot Copilot AI mentioned this pull request May 24, 2025
termoshtt added a commit that referenced this pull request May 24, 2025
@termoshtt termoshtt merged commit 57892db into Jij-Inc:main May 24, 2025
3 of 4 checks passed
@termoshtt
Copy link
Member

Merged via #27, Thanks!

@LockedThread
Copy link
Contributor Author

Merged via #27, Thanks!

Awesome, thanks for merging this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants