Skip to content

Commit

Permalink
[native] Return tuples instead of lists in CST (#631)
Browse files Browse the repository at this point in the history
  • Loading branch information
zsol authored Jan 28, 2022
1 parent 8ed3a9c commit dba8296
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions native/libcst_derive/src/into_py.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ fn fields_to_kwargs(fields: &Fields, is_enum: bool) -> quote::__private::TokenSt
let mut rust_varnames = vec![];
let mut optional_py_varnames = vec![];
let mut optional_rust_varnames = vec![];
let mut vec_py_varnames = vec![];
let mut vec_rust_varnames = vec![];
match &fields {
Fields::Named(FieldsNamed { named, .. }) => {
for field in named.iter() {
Expand Down Expand Up @@ -138,12 +140,23 @@ fn fields_to_kwargs(fields: &Fields, is_enum: bool) -> quote::__private::TokenSt
}
}
}
if let Type::Path(TypePath { path, .. }) = &field.ty {
if let Some(first) = path.segments.first() {
if first.ident == "Vec" {
vec_py_varnames.push(pyname);
vec_rust_varnames.push(rustname);
continue;
}
}
}
py_varnames.push(pyname);
rust_varnames.push(rustname);
}
}
}
empty_kwargs = py_varnames.is_empty() && optional_py_varnames.is_empty();
empty_kwargs = py_varnames.is_empty()
&& optional_py_varnames.is_empty()
&& vec_py_varnames.is_empty();
}
Fields::Unnamed(FieldsUnnamed { unnamed, .. }) => {
if unnamed.first().is_some() {
Expand All @@ -163,11 +176,22 @@ fn fields_to_kwargs(fields: &Fields, is_enum: bool) -> quote::__private::TokenSt
let optional_pairs = quote! {
#(#optional_rust_varnames.map(|x| (stringify!(#optional_py_varnames), x.into_py(py))),)*
};
let vec_pairs = quote! {
#(Some((
stringify!(#vec_py_varnames),
pyo3::IntoPy::<pyo3::PyObject>::into_py(
pyo3::types::PyTuple::new(
py,
#vec_rust_varnames.into_iter().map(|x| x.into_py(py)),
),
py,
))),)*
};
if empty_kwargs {
quote! { pyo3::types::PyDict::new(py) }
} else {
quote! {
[ #kwargs_pairs #optional_pairs ]
[ #kwargs_pairs #optional_pairs #vec_pairs ]
.iter()
.filter(|x| x.is_some())
.map(|x| x.as_ref().unwrap())
Expand Down

0 comments on commit dba8296

Please sign in to comment.