Skip to content

Commit

Permalink
fix stubs and use ?
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-o-how committed Sep 9, 2024
1 parent 2723e1f commit 34ab0f9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 38 deletions.
57 changes: 21 additions & 36 deletions crates/chia_py_streamable_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,13 @@ pub fn py_streamable_macro(input: proc_macro::TokenStream) -> proc_macro::TokenS
pub fn from_json_dict(cls: &pyo3::Bound<'_, pyo3::types::PyType>, json_dict: &pyo3::Bound<pyo3::PyAny>) -> pyo3::PyResult<pyo3::PyObject> {
use pyo3::prelude::PyAnyMethods;
use pyo3::IntoPy;
let rust_obj = <Self as #crate_name::from_json_dict::FromJsonDict>::from_json_dict(json_dict);
match rust_obj {
Ok(obk) => {
pyo3::Python::with_gil(|py| {
// Convert result into potential child class
// let instance = cls.call(py, (rust_obj,))?;
let instance = cls.call_method1("from_parent", (obk.into_py(py),))?;
Ok(instance.into_py(py))
})
},
Err(e) => Err(e)
}
let rust_obj = <Self as #crate_name::from_json_dict::FromJsonDict>::from_json_dict(json_dict)?;
pyo3::Python::with_gil(|py| {
// Convert result into potential child class
// let instance = cls.call(py, (rust_obj,))?;
let instance = cls.call_method1("from_parent", (rust_obj.into_py(py),))?;
Ok(instance.into_py(py))
})
}

pub fn to_json_dict(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::PyObject> {
Expand All @@ -198,18 +193,13 @@ pub fn py_streamable_macro(input: proc_macro::TokenStream) -> proc_macro::TokenS
let slice = unsafe {
std::slice::from_raw_parts(blob.buf_ptr() as *const u8, blob.len_bytes())
};
let rust_obj = <Self as #crate_name::Streamable>::from_bytes(slice);
match rust_obj {
Ok(obk) => {
pyo3::Python::with_gil(|py| {
// Convert result into potential child class
// let instance = cls.call(py, (rust_obj,))?;
let instance = cls.call_method1("from_parent", (obk.into_py(py),))?;
Ok(instance.into_py(py))
})
},
Err(e) => Err(<#crate_name::chia_error::Error as Into<pyo3::PyErr>>::into(e))
}
let rust_obj = <Self as #crate_name::Streamable>::from_bytes(slice)?;
pyo3::Python::with_gil(|py| {
// Convert result into potential child class
// let instance = cls.call(py, (rust_obj,))?;
let instance = cls.call_method1("from_parent", (rust_obj.into_py(py),))?;
Ok(instance.into_py(py))
})
}

#[classmethod]
Expand All @@ -223,18 +213,13 @@ pub fn py_streamable_macro(input: proc_macro::TokenStream) -> proc_macro::TokenS
let slice = unsafe {
std::slice::from_raw_parts(blob.buf_ptr() as *const u8, blob.len_bytes())
};
let rust_obj = <Self as #crate_name::Streamable>::from_bytes_unchecked(slice).map_err(|e| <#crate_name::chia_error::Error as Into<pyo3::PyErr>>::into(e));
match rust_obj {
Ok(obk) => {
pyo3::Python::with_gil(|py| {
// Convert result into potential child class
// let instance = cls.call(py, (rust_obj,))?;
let instance = cls.call_method1("from_parent", (obk.into_py(py),))?;
Ok(instance.into_py(py))
})
},
Err(e) => Err(e)
}
let rust_obj = <Self as #crate_name::Streamable>::from_bytes_unchecked(slice).map_err(|e| <#crate_name::chia_error::Error as Into<pyo3::PyErr>>::into(e))?;
pyo3::Python::with_gil(|py| {
// Convert result into potential child class
// let instance = cls.call(py, (rust_obj,))?;
let instance = cls.call_method1("from_parent", (rust_obj.into_py(py),))?;
Ok(instance.into_py(py))
})
}

// returns the type as well as the number of bytes read from the buffer
Expand Down
2 changes: 1 addition & 1 deletion wheel/generate_type_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def parse_rust_source(filename: str, upper_case: bool) -> List[Tuple[str, List[s
"def uncurry(self) -> Tuple[ChiaProgram, ChiaProgram]: ...",
],
"SpendBundle": [
"@classmethod\n def aggregate(sbs: List[SpendBundle]) -> SpendBundle: ...",
"@classmethod\n def aggregate(cls, sbs: List[SpendBundle]) -> SpendBundle: ...",
"def name(self) -> bytes32: ...",
"def removals(self) -> List[Coin]: ...",
"def additions(self) -> List[Coin]: ...",
Expand Down
2 changes: 1 addition & 1 deletion wheel/python/chia_rs/chia_rs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2342,7 +2342,7 @@ class SpendBundle:
coin_spends: List[CoinSpend]
aggregated_signature: G2Element
@classmethod
def aggregate(sbs: List[SpendBundle]) -> SpendBundle: ...
def aggregate(cls, sbs: List[SpendBundle]) -> SpendBundle: ...
def name(self) -> bytes32: ...
def removals(self) -> List[Coin]: ...
def additions(self) -> List[Coin]: ...
Expand Down

0 comments on commit 34ab0f9

Please sign in to comment.