diff --git a/engine/Cargo.lock b/engine/Cargo.lock index 521a40c9e..72ad264f2 100644 --- a/engine/Cargo.lock +++ b/engine/Cargo.lock @@ -909,7 +909,6 @@ dependencies = [ "serde_json", "tokio", "tracing-subscriber", - "valuable", ] [[package]] diff --git a/engine/language_client_python/Cargo.toml b/engine/language_client_python/Cargo.toml index 28b2cf6c8..4c0ab7c76 100644 --- a/engine/language_client_python/Cargo.toml +++ b/engine/language_client_python/Cargo.toml @@ -51,7 +51,6 @@ tracing-subscriber = { version = "0.3.18", features = [ "env-filter", "valuable", ] } -valuable = { version = "0.1.0", features = ["derive"] } [build-dependencies] pyo3-build-config = "0.21.2" diff --git a/engine/language_client_python/src/lib.rs b/engine/language_client_python/src/lib.rs index 07e357c66..1818fa7dc 100644 --- a/engine/language_client_python/src/lib.rs +++ b/engine/language_client_python/src/lib.rs @@ -19,51 +19,6 @@ fn invoke_runtime_cli(py: Python) -> PyResult<()> { ) .map_err(errors::BamlError::from_anyhow) } -#[pyo3::prelude::pyclass(module = "baml_py.baml_py")] -pub struct LoremIpsum { - #[allow(dead_code)] - pub(crate) inner: String, -} - -#[pyo3::prelude::pymethods] -impl LoremIpsum { - #[new] - pub fn new() -> Self { - Self { - inner: "Lorem ipsum dolor sit amet".to_string(), - } - } - - // pub fn __getnewargs__<'py>( - // &self, - // py: Python<'py>, - // ) -> PyResult> { - // println!("__getnewargs__ LoremIpsum placeholder"); - // Ok(pyo3::types::PyTuple::empty_bound(py)) - // } - - #[classmethod] - pub fn __get_pydantic_core_schema__( - _cls: Bound<'_, pyo3::types::PyType>, - _source_type: Bound<'_, pyo3::types::PyAny>, - _handler: Bound<'_, pyo3::types::PyAny>, - ) -> PyResult { - Python::with_gil(|py| { - let code = r#" -from pydantic_core import core_schema, SchemaValidator - -ret = core_schema.str_schema() - "#; - // py.run(code, None, Some(ret_dict)); - let fun: pyo3::Py = - PyModule::from_code_bound(py, code, "pretend-file", "pretend-module")? - .getattr("ret")? - .into(); - use pyo3::ToPyObject; - Ok(fun.to_object(py)) - }) - } -} pub(crate) const MODULE_NAME: &str = "baml_py.baml_py"; @@ -99,8 +54,6 @@ fn baml_py(m: Bound<'_, PyModule>) -> PyResult<()> { } } - m.add_class::()?; - m.add_class::()?; m.add_class::()?; @@ -123,22 +76,7 @@ fn baml_py(m: Bound<'_, PyModule>) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(invoke_runtime_cli))?; - // m.add( - // "BamlValidationError", - // m.py().get_type_bound::(), - // )?; - // m.add_class::()?; errors::errors(&m)?; Ok(()) } - -mod test { - #[test] - fn test_inspect() { - assert_eq!( - crate::MODULE_NAME, - format!("baml_py.{}", stringify!(baml_asdfpy)) - ); - } -} diff --git a/engine/language_client_python/src/types/audio.rs b/engine/language_client_python/src/types/audio.rs index a0f9dc7b9..2ce520f6c 100644 --- a/engine/language_client_python/src/types/audio.rs +++ b/engine/language_client_python/src/types/audio.rs @@ -1,6 +1,6 @@ use baml_types::BamlMediaContent; use pyo3::prelude::{pymethods, PyResult}; -use pyo3::types::PyType; +use pyo3::types::{PyTuple, PyType}; use pyo3::{Bound, PyAny, PyObject, Python}; use pythonize::{depythonize_bound, pythonize}; @@ -50,6 +50,20 @@ impl BamlAudioPy { } } + /// Defines the default constructor: https://pyo3.rs/v0.23.3/class#constructor + /// + /// Used for `pickle.load`: https://docs.python.org/3/library/pickle.html#object.__getnewargs__ + #[new] + pub fn py_new(data: PyObject, py: Python<'_>) -> PyResult { + Self::baml_deserialize(data, py) + } + + /// Used for `pickle.dump`: https://docs.python.org/3/library/pickle.html#object.__getnewargs__ + pub fn __getnewargs__<'py>(&self, py: Python<'py>) -> PyResult> { + let o = self.baml_serialize(py)?; + Ok(PyTuple::new_bound(py, vec![o])) + } + pub fn __repr__(&self) -> String { match &self.inner.content { BamlMediaContent::Url(url) => { diff --git a/engine/language_client_python/src/types/image.rs b/engine/language_client_python/src/types/image.rs index 24b5aa5fb..01dbae941 100644 --- a/engine/language_client_python/src/types/image.rs +++ b/engine/language_client_python/src/types/image.rs @@ -49,26 +49,18 @@ impl BamlImagePy { } } - // pub fn __setstate__(&mut self, state: PyObject, py: Python<'_>) -> PyResult<()> { - // log::info!("Setting state"); - // *self = BamlImagePy::baml_deserialize(state, py)?; - // Ok(()) - // } - - // pub fn __getstate__(&self, py: Python<'_>) -> PyResult { - // log::info!("Getting state"); - // self.baml_serialize(py) - // } - + /// Defines the default constructor: https://pyo3.rs/v0.23.3/class#constructor + /// + /// Used for `pickle.load`: https://docs.python.org/3/library/pickle.html#object.__getnewargs__ #[new] - pub fn py_new(_py: Python<'_>) -> PyResult { - Ok(BamlImagePy::from_url( - "https://example.com/screenshot.png".to_string(), - )) + pub fn py_new(data: PyObject, py: Python<'_>) -> PyResult { + Self::baml_deserialize(data, py) } + /// Used for `pickle.dump`: https://docs.python.org/3/library/pickle.html#object.__getnewargs__ pub fn __getnewargs__<'py>(&self, py: Python<'py>) -> PyResult> { - Ok(PyTuple::empty_bound(py)) + let o = self.baml_serialize(py)?; + Ok(PyTuple::new_bound(py, vec![o])) } pub fn __repr__(&self) -> String { diff --git a/engine/language_client_python/src/types/media_repr.rs b/engine/language_client_python/src/types/media_repr.rs index 0b2dc94f8..5e07688ff 100644 --- a/engine/language_client_python/src/types/media_repr.rs +++ b/engine/language_client_python/src/types/media_repr.rs @@ -73,6 +73,9 @@ impl TryInto for &BamlMedia { /// can't implement this in internal_monkeypatch without adding a hard dependency /// on pydantic. And we don't want to do _that_, because that will make it harder /// to implement output_type python/vanilla in the future. +/// +/// See docs: +/// https://docs.pydantic.dev/latest/concepts/types/#customizing-validation-with-__get_pydantic_core_schema__ pub fn __get_pydantic_core_schema__( _cls: Bound<'_, PyType>, _source_type: Bound<'_, PyAny>, diff --git a/typescript/.vscode/launch.json b/typescript/.vscode/launch.json index 57503ec23..e9c6036dd 100644 --- a/typescript/.vscode/launch.json +++ b/typescript/.vscode/launch.json @@ -1,8 +1,6 @@ { "version": "0.2.0", "configurations": [ - - { "name": "Launch VS Code extension", "type": "extensionHost",