-
Notifications
You must be signed in to change notification settings - Fork 8
Extend Deserialization Support to Pydantic Models and Dataclasses #23
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
Open
LockedThread
wants to merge
9
commits into
Jij-Inc:main
Choose a base branch
from
LockedThread:feat/add-pydantic-dataclasses-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1aacb80
Add dataclass and pydantic support
LockedThread 96c7ac5
Remove dataclass and pydantic support from defaults
LockedThread 1cdf01c
remove unnecessary comments
LockedThread dbe484e
Merge branch 'main' into feat/add-pydantic-dataclasses-support
LockedThread afdcd06
Merge branch 'main' into feat/add-pydantic-dataclasses-support
LockedThread 6132759
Run rustfmt
LockedThread 6e417a2
Merge branch 'main' into pr/LockedThread/23
termoshtt 5c303f4
Fix deprecation warnings and dead code in pydantic/dataclass support
termoshtt 41b4c6b
Merge branch 'main' into pr/LockedThread/23
termoshtt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| use once_cell::sync::OnceCell; | ||
| use pyo3::{types::*, Bound, IntoPyObject, Py, PyResult, Python}; | ||
|
|
||
| // Individual OnceCell instances for each cached item | ||
| #[cfg(feature = "dataclass_support")] | ||
| static DATACLASSES_MODULE: OnceCell<Py<PyAny>> = OnceCell::new(); | ||
| #[cfg(feature = "dataclass_support")] | ||
| static IS_DATACLASS_FN: OnceCell<Py<PyAny>> = OnceCell::new(); | ||
|
|
||
| #[cfg(feature = "pydantic_support")] | ||
| static PYDANTIC_MODULE: OnceCell<Py<PyAny>> = OnceCell::new(); | ||
| #[cfg(feature = "pydantic_support")] | ||
| static PYDANTIC_BASE_MODEL: OnceCell<Py<PyAny>> = OnceCell::new(); | ||
|
|
||
| #[cfg(feature = "pydantic_support")] | ||
| fn is_module_installed(py: Python, module_name: &str) -> PyResult<bool> { | ||
| match PyModule::import(py, module_name) { | ||
| Ok(_) => Ok(true), | ||
| Err(err) => { | ||
| if err.is_instance_of::<pyo3::exceptions::PyModuleNotFoundError>(py) { | ||
| Ok(false) | ||
| } else { | ||
| Err(err) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "dataclass_support")] | ||
| pub fn is_dataclass(py: Python, obj: &Bound<'_, PyAny>) -> PyResult<bool> { | ||
| // Initialize the dataclasses module if needed | ||
| if DATACLASSES_MODULE.get().is_none() { | ||
| let dataclasses = PyModule::import(py, "dataclasses")?; | ||
| let _ = DATACLASSES_MODULE.set(dataclasses.into()); | ||
| } | ||
|
|
||
| // Initialize the is_dataclass function if needed | ||
| let is_dataclass_fn = if let Some(fn_obj) = IS_DATACLASS_FN.get() { | ||
| fn_obj | ||
| } else { | ||
| let dataclasses = DATACLASSES_MODULE | ||
| .get() | ||
| .ok_or_else(|| { | ||
| pyo3::exceptions::PyRuntimeError::new_err("Dataclasses module not initialized") | ||
| })? | ||
| .bind(py); | ||
| let is_dataclass_fn: Py<PyAny> = dataclasses | ||
| .getattr("is_dataclass")? | ||
| .into_pyobject(py)? | ||
| .into(); | ||
| // Safe to unwrap because we know the value is set | ||
| let _ = IS_DATACLASS_FN.set(is_dataclass_fn); | ||
| IS_DATACLASS_FN.get().ok_or_else(|| { | ||
| pyo3::exceptions::PyRuntimeError::new_err("Failed to initialize is_dataclass function") | ||
| })? | ||
| }; | ||
|
|
||
| // Execute the function | ||
| let result = is_dataclass_fn.bind(py).call1((obj,))?; | ||
| result.extract() | ||
| } | ||
| #[cfg(feature = "pydantic_support")] | ||
| pub fn is_pydantic_base_model(py: Python, obj: &Bound<'_, PyAny>) -> PyResult<bool> { | ||
| // First check if pydantic is installed | ||
| if !is_module_installed(py, "pydantic")? { | ||
| return Ok(false); | ||
| } | ||
|
|
||
| // Initialize pydantic module if needed | ||
| if PYDANTIC_MODULE.get().is_none() { | ||
| let pydantic = PyModule::import(py, "pydantic")?; | ||
| // Safe to unwrap because we know the value is empty | ||
termoshtt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let _ = PYDANTIC_MODULE.set(pydantic.into()); | ||
| } | ||
|
|
||
| // Initialize BaseModel if needed | ||
| let base_model = if let Some(model) = PYDANTIC_BASE_MODEL.get() { | ||
| model | ||
| } else { | ||
| let pydantic = PYDANTIC_MODULE | ||
| .get() | ||
| .ok_or_else(|| { | ||
| pyo3::exceptions::PyRuntimeError::new_err("Pydantic module not initialized") | ||
| })? | ||
| .bind(py); | ||
| let base_model: Py<PyAny> = pydantic.getattr("BaseModel")?.into_pyobject(py)?.into(); | ||
| // Safe to unwrap because we know the value is empty | ||
termoshtt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let _ = PYDANTIC_BASE_MODEL.set(base_model); | ||
| PYDANTIC_BASE_MODEL.get().ok_or_else(|| { | ||
| pyo3::exceptions::PyRuntimeError::new_err("Failed to initialize BaseModel") | ||
| })? | ||
| }; | ||
|
|
||
| // Check if object is instance of BaseModel | ||
| obj.is_instance(base_model.bind(py)) | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.