Skip to content

Extend Deserialization Support to Pydantic Models and Dataclasses#23

Open
LockedThread wants to merge 9 commits intoJij-Inc:mainfrom
LockedThread:feat/add-pydantic-dataclasses-support
Open

Extend Deserialization Support to Pydantic Models and Dataclasses#23
LockedThread wants to merge 9 commits intoJij-Inc:mainfrom
LockedThread:feat/add-pydantic-dataclasses-support

Conversation

@LockedThread
Copy link
Copy Markdown
Contributor

This PR builds on the previous work by extending the serialization and deserialization capabilities to include support for Pydantic models and Python dataclasses. The new commit 1aacb80 introduces functionality that further bridges Python's dynamic data structures with Rust’s type-safe ecosystem.

Overview

  • Pydantic Support:
    Pydantic models are now supported out-of-the-box. The implementation detects when a Python object is a Pydantic model and appropriately serializes its underlying data.

  • Dataclasses Support:
    Similarly, Python dataclasses are now handled seamlessly. The logic checks for dataclass instances and extracts their fields for serialization.

Implementation Details

  • Type Checking:
    The implementation introduces checks to determine if an object is an instance of a Pydantic model or a dataclass. This is performed before falling back to the general Python object handling.

  • Data Extraction:

    • For Pydantic models, the model's dict representation is extracted and serialized.
    • For dataclasses, the field values are similarly extracted, ensuring that both explicit, default, and nested values are considered during the deserialization process.

Testing

  • Unit Tests:
    New tests have been added to verify:
    • Dserialization cycles for Pydantic models and dataclasses.

Conclusion

This PR significantly enhances the serde-pyobject library by incorporating first-class support for Pydantic models and Python dataclasses. These changes make it easier for developers to work with Python’s modern data modeling tools in a Rust environment, ensuring a smoother and more robust interoperation between the two ecosystems.

Please review the changes and share any feedback. Further improvements and refinements can be addressed in subsequent PRs.

@LockedThread
Copy link
Copy Markdown
Contributor Author

Ignore until #22 is merged.

@LockedThread LockedThread changed the title Extend Serialization Support to Pydantic Models and Dataclasses Extend Deserialization Support to Pydantic Models and Dataclasses Apr 2, 2025
@LockedThread LockedThread marked this pull request as ready for review May 24, 2025 20:29
@LockedThread
Copy link
Copy Markdown
Contributor Author

@termoshtt Thank you for merging #22, this PR depends on the changes of #22. Now that it's merged can you please review this? Let me know if you have any requested modifications.

@termoshtt termoshtt self-requested a review May 25, 2025 01:07
@LockedThread
Copy link
Copy Markdown
Contributor Author

@termoshtt Any updates?

termoshtt and others added 2 commits October 17, 2025 01:59
- Replace deprecated pyo3::PyObject with Py<PyAny>
- Add #[cfg(feature = "pydantic_support")] to is_module_installed to fix unused function warning
- Replace deprecated Python::with_gil with Python::attach in tests

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings October 16, 2025 17:07
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR extends the serialization and deserialization capabilities of serde-pyobject to support Pydantic models and Python dataclasses, enabling seamless interoperation between Python's modern data modeling tools and Rust's type system.

  • Adds type checking for Pydantic models and dataclasses during deserialization
  • Implements module caching for performance optimization
  • Introduces feature flags for optional Pydantic and dataclass support

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
Cargo.toml Adds optional once_cell dependency and feature flags for dataclass/pydantic support
src/lib.rs Conditionally includes the new py_module_cache module
src/py_module_cache.rs Implements cached module imports and type checking for dataclasses and pydantic
src/de.rs Extends deserializer to handle pydantic models and dataclasses before generic object handling
tests/check_revertible.rs Adds comprehensive tests for pydantic and dataclass serialization/deserialization

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@termoshtt
Copy link
Copy Markdown
Member

Thanks PR with excellent idea. I merge this PR with some fix with #33 including omitting Python module caching feature. I guess these are well cached in Python side, but did not measure actual benchmark. Do you have any result for it? If it causes some performance issue, I will restore caching mechanism.

@LockedThread
Copy link
Copy Markdown
Contributor Author

Thanks PR with excellent idea. I merge this PR with some fix with #33 including omitting Python module caching feature. I guess these are well cached in Python side, but did not measure actual benchmark. Do you have any result for it? If it causes some performance issue, I will restore caching mechanism.

I figured that the caching is like the same thing as a file importing a module versus listing the module import inside of the function you are calling.

Importing a module does actually cause code to execute for that module's __init__.py file.

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