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

Function default values #27

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion examples/os_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn main() -> pyo3::PyResult<()> {
// Get the current working directory via "os" Python module
let current_dir = os::getcwd(py)?;
// Get the relative path to the Python executable via "posixpath" Python module
let relpath_to_python_exe = posixpath::relpath(py, python_exe_path, current_dir)?;
let relpath_to_python_exe = posixpath::relpath(py, python_exe_path, Some(current_dir))?;

println!("Relative path to Python executable: '{relpath_to_python_exe}'");
Ok(())
Expand Down
40 changes: 35 additions & 5 deletions pyo3_bindgen_engine/src/syntax/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,15 +376,35 @@
.iter()
.zip(param_idents.iter())
.map(|(param, param_ident)| {
param
let bind = param
.annotation
.preprocess_borrowed(param_ident, local_types)
.preprocess_borrowed(param_ident, local_types);
if param.default.is_some() && !matches!(param.annotation, Type::Optional(_)) {
let option_ident = quote::format_ident!("optional_{}", param_ident);
quote::quote! {
let #option_ident = #param_ident.is_some();
#bind
}
} else {
bind
}
})
.collect();
let param_types: Vec<proc_macro2::TokenStream> = self
.parameters
.iter()
.map(|param| Result::Ok(param.annotation.clone().into_rs_borrowed(local_types)))
.map(|param| {
let local_type = param.annotation.clone().into_rs_borrowed(local_types);
let res =
if param.default.is_some() && !matches!(param.annotation, Type::Optional(_)) {
quote::quote! {
Option<#local_type>
}
} else {
local_type
};
Result::Ok(res)
})
.collect::<Result<Vec<_>>>()?;
let return_type = self.return_annotation.clone().into_rs_owned(local_types);
let fn_contract = match &self.typ {
Expand Down Expand Up @@ -568,6 +588,10 @@
.iter()
.map(|param| Ok(Ident::from_py(&format!("p_{}", param.name)).try_into()?))
.collect::<Result<_>>()?;
let keyword_args_idents_optional: Vec<syn::Ident> = keyword_args_idents
.iter()
.map(|param| quote::format_ident!("optional_{}", param))
.collect::<_>();
let var_keyword_args_ident: Option<syn::Ident> = self
.parameters
.iter()
Expand All @@ -580,11 +604,15 @@
#var_keyword_args_ident
}
} else {
//TODO::
//let option_ident: syn::Ident = Ident::from_py(&format!("optional_{}", param.name)).try_into().unwrap();
quote::quote! {
{
let __internal__kwargs = #var_keyword_args_ident;
#(
::pyo3::types::PyDictMethods::set_item(&__internal__kwargs, ::pyo3::intern!(py, #keyword_args_names), #keyword_args_idents);
if #keyword_args_idents_optional {
::pyo3::types::PyDictMethods::set_item(&__internal__kwargs, ::pyo3::intern!(py, #keyword_args_names), #keyword_args_idents);
};

Check warning on line 615 in pyo3_bindgen_engine/src/syntax/function.rs

View check run for this annotation

Codecov / codecov/patch

pyo3_bindgen_engine/src/syntax/function.rs#L613-L615

Added lines #L613 - L615 were not covered by tests
)*
__internal__kwargs
}
Expand All @@ -599,7 +627,9 @@
{
let __internal__kwargs = ::pyo3::types::PyDict::new_bound(py);
#(
::pyo3::types::PyDictMethods::set_item(&__internal__kwargs, ::pyo3::intern!(py, #keyword_args_names), #keyword_args_idents);
if #keyword_args_idents_optional {
::pyo3::types::PyDictMethods::set_item(&__internal__kwargs, ::pyo3::intern!(py, #keyword_args_names), #keyword_args_idents);
};
)*
__internal__kwargs
}
Expand Down