Extend Deserialization Support to Pydantic Models and Dataclasses#23
Extend Deserialization Support to Pydantic Models and Dataclasses#23LockedThread wants to merge 9 commits intoJij-Inc:mainfrom
Conversation
|
Ignore until #22 is merged. |
|
@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 Any updates? |
- 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>
There was a problem hiding this comment.
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.
|
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 |
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:
Testing
New tests have been added to verify:
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.