Skip to content

Commit

Permalink
fix deprecation warning for trailing optional on #[setter] functions
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Jul 2, 2024
1 parent f3603a0 commit 65a8ad3
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions newsfragments/4304.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix invalid deprecation warning for trailing optional on `#[setter]` function.
1 change: 1 addition & 0 deletions pyo3-macros-backend/src/deprecations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl<'ctx> ToTokens for Deprecations<'ctx> {

pub(crate) fn deprecate_trailing_option_default(spec: &FnSpec<'_>) -> TokenStream {
if spec.signature.attribute.is_none()
&& spec.tp.signature_attribute_allowed()
&& spec.signature.arguments.iter().any(|arg| {
if let FnArg::Regular(arg) = arg {
arg.option_wrapped_type.is_some()
Expand Down
17 changes: 16 additions & 1 deletion pyo3-macros-backend/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,18 @@ impl FnType {
}
}

pub fn signature_attribute_allowed(&self) -> bool {
match self {
FnType::Fn(_)
| FnType::FnNew
| FnType::FnStatic
| FnType::FnClass(_)
| FnType::FnNewClass(_)
| FnType::FnModule(_) => true,
FnType::Getter(_) | FnType::Setter(_) | FnType::ClassAttribute => false,
}
}

pub fn self_arg(
&self,
cls: Option<&syn::Type>,
Expand Down Expand Up @@ -1096,15 +1108,18 @@ fn ensure_signatures_on_valid_method(
if let Some(signature) = signature {
match fn_type {
FnType::Getter(_) => {
debug_assert!(!fn_type.signature_attribute_allowed());
bail_spanned!(signature.kw.span() => "`signature` not allowed with `getter`")
}
FnType::Setter(_) => {
debug_assert!(!fn_type.signature_attribute_allowed());
bail_spanned!(signature.kw.span() => "`signature` not allowed with `setter`")
}
FnType::ClassAttribute => {
debug_assert!(!fn_type.signature_attribute_allowed());
bail_spanned!(signature.kw.span() => "`signature` not allowed with `classattr`")
}
_ => {}
_ => debug_assert!(fn_type.signature_attribute_allowed()),
}
}
if let Some(text_signature) = text_signature {
Expand Down
36 changes: 36 additions & 0 deletions tests/test_getter_setter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,39 @@ fn frozen_py_field_get() {
py_run!(py, inst, "assert inst.value == 'value'");
});
}

#[test]
fn test_optional_setter() {
#[pyclass]
struct SimpleClass {
field: Option<u32>,
}

#[pymethods]
impl SimpleClass {
#[getter]
fn get_field(&self) -> Option<u32> {
self.field
}

#[setter]
fn set_field(&mut self, field: Option<u32>) {
self.field = field;
}
}

Python::with_gil(|py| {
let instance = Py::new(py, SimpleClass { field: None }).unwrap();
py_run!(py, instance, "assert instance.field is None");
py_run!(
py,
instance,
"instance.field = 42; assert instance.field == 42"
);
py_run!(
py,
instance,
"instance.field = None; assert instance.field is None"
);
})
}

0 comments on commit 65a8ad3

Please sign in to comment.