Skip to content
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

[native] Return tuples instead of lists in CST nodes #631

Merged
merged 1 commit into from
Jan 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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