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

Fix clippy::redundant_closure lint firing for pyfunction defaults #2990

Merged
merged 1 commit into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions newsfragments/2990.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix `clippy::redundant_closure` lint on default arguments in `#[pyo3(signature = (...))]` annotations.
10 changes: 9 additions & 1 deletion pyo3-macros-backend/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,13 @@ fn impl_arg_param(
let tokens = if let Some(expr_path) = arg.attrs.from_py_with.as_ref().map(|attr| &attr.value) {
if let Some(default) = default {
quote_arg_span! {
_pyo3::impl_::extract_argument::from_py_with_with_default(#arg_value, #name_str, #expr_path, || #default)?
#[allow(clippy::redundant_closure)]
_pyo3::impl_::extract_argument::from_py_with_with_default(
#arg_value,
#name_str,
#expr_path,
|| #default
)?
}
} else {
quote_arg_span! {
Expand All @@ -220,6 +226,7 @@ fn impl_arg_param(
}
} else if arg.optional.is_some() {
quote_arg_span! {
#[allow(clippy::redundant_closure)]
_pyo3::impl_::extract_argument::extract_optional_argument(
#arg_value,
&mut { _pyo3::impl_::extract_argument::FunctionArgumentHolder::INIT },
Expand All @@ -229,6 +236,7 @@ fn impl_arg_param(
}
} else if let Some(default) = default {
quote_arg_span! {
#[allow(clippy::redundant_closure)]
_pyo3::impl_::extract_argument::extract_argument_with_default(
#arg_value,
&mut { _pyo3::impl_::extract_argument::FunctionArgumentHolder::INIT },
Expand Down
17 changes: 17 additions & 0 deletions tests/test_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1543,3 +1543,20 @@ fn test_option_pyclass_arg() {
.is_ok());
})
}

#[test]
#[allow(non_snake_case)] // FIXME __pyfunction__foo expanded symbol is not snake case
fn test_issue_2988() {
#[pyfunction]
#[pyo3(signature = (
_data = vec![],
_data2 = vec![],
))]
pub fn _foo(
_data: Vec<i32>,
// The from_py_with here looks a little odd, we just need some way
// to encourage the macro to expand the from_py_with default path too
#[pyo3(from_py_with = "PyAny::extract")] _data2: Vec<i32>,
) {
}
}