-
Notifications
You must be signed in to change notification settings - Fork 784
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
Crazy idea: serde support #566
Comments
We actually wanted something like this, mostly when you want to return something which is 'data', but don't want (or can't) create a dedicated python wrapper for each struct. |
@birkenfeld I think that this sort of thing is a good idea, but is it really something that belongs in PyO3? I think it makes sense to leave the core C API wrappers in PyO3 and break out useful additional functionality into their own crates. |
I took a stab at something like this, by implementing the custom serde traits, but then ran into lifetime issues with all the python data since everything relies on the interpreter lifetime. Not that it probably isn't possible, but It'll take someone with more experience than me. I ended up just dumping everything to a raw json string and passing it over, which is a trivial amount of code on both python and rust sides, but not an elegant solution. |
I've made a crate to help deal with this. It let's you derive the necessary traits for PyO3 to convert a dict into your struct, as long as it knows how to convert every field. I plan on having serde-like field attributes in the future, but if you're not using them the base functionality should beat passing things around as json. |
I would encourage you all not to overthink this. So allow me to step back and do exactly that at a more abstract level I guess... :-/ Who is your audience? Is it people who are primarily writing in Rust or Python? What are they struggling with? Are they struggling with learning one language or the other, or learning how to call something from Python using e.g. What are they trying to accomplish? Are they trying to translate from one language to the other? Do they feel the "need for speed" and contemplating trying something other than C? Are they primarily Rust programmers looking for a more transparent "glue" for some larger problem (see the popularity of embedded Lua for various tasks)? I have a brick in the wall to offer as someone primarily writing in Python who had an optimization problem and stumbled across (A friend of mine who write primarily in Rust brought this issue to my attention.) |
One for the folks watching this: I've made a WIP crate which targets a full serde implementation for PyO3. See https://github.com/davidhewitt/pythonize The idea is that it should be pretty much equivalent to Note that unlike dict-derive this means that it doesn't integrate very nicely with types that already implement At the moment only the to-Python direction is implemented, but I'd be interested in taking feedback / help finishing this off. |
I had a similar use-case, convert a |
This is exactly what I needed. |
@kivo360 @leplatrem An interesting question I was wondering this week is whether we should provide serde wrappers for types like |
Is there any chance pythonize would become a native part of PyO3? |
I haven't seen a need for it to become part of the main crate; what's the advantage that you see from doing so? |
I'm new to PyO3 but even from just porting one simple app it seemed logical a native serialiser/deserialiser would be a fundamental core feature for efficient workflows, and serde is seemingly the defacto rust standard for this. |
That's a reasonable opinion. There's no performance or API disadvantages by having |
Is there still a compile time disadvantage if it's implemented as a 'feature'? |
Probably not a huge difference for users, however the PyO3 CI already takes a long time, and I count that as a compile time disadvantage too! Also worth bearing in mind that I hoped that anything which merged into PyO3 would have a solution for davidhewitt/pythonize#1 so that there would only be one integrated way to convert Rust -> Python objects, rather than two different mechanisms in the same crate. I really do hold the opinion that |
Hi, is there any chance to make pythonize work automatically? Or, what is the best practice when returning a complex nested struct? Thanks! For example, I hope to use sth like: #[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Sample {
foo: String,
bar: Option<usize>
}
#[pymethods]
impl MyClass {
fn f() -> Sample { ... }
} ... and it auto works (instead of manually calling |
I am not sure if we can make this work transparently as the trait bound |
A few times now, I had to take in complex-ish Python data and convert it into an equivalent in Rust.
FromPyObject
already does the basic types, but I'd also like to convert e.g. nested dicts with certain keys into structs.Do you think it would make sense to let
serde
handle this? It already has all the options you'd like, e.g. flattening of sub-structure and renaming of things. For structs, it should support both dicts (taking keys) and other objects (taking attributes).The "ser" direction could also be supported, although I don't need it, and it would probably only construct dicts for structs.
The text was updated successfully, but these errors were encountered: