diff --git a/CHANGELOG.md b/CHANGELOG.md index c73b7e73..50d5d662 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. * Clear error indicator when the exception is handled on the Rust side. [#719](https://github.com/PyO3/pyo3/pull/719) * Fixed unsoundness of subclassing. [#683](https://github.com/PyO3/pyo3/pull/683). +* Usage of raw identifiers with `#[pyo3(set)]`. [#745](https://github.com/PyO3/pyo3/pull/745) ### Removed diff --git a/pyo3-derive-backend/src/pyclass.rs b/pyo3-derive-backend/src/pyclass.rs index e70694eb..d4d9e564 100644 --- a/pyo3-derive-backend/src/pyclass.rs +++ b/pyo3-derive-backend/src/pyclass.rs @@ -427,7 +427,7 @@ fn impl_descriptors( .flat_map(|&(ref field, ref fns)| { fns.iter() .map(|desc| { - let name = field.ident.clone().unwrap(); + let name = field.ident.as_ref().unwrap(); let field_ty = &field.ty; match *desc { FnType::Getter => { @@ -441,7 +441,7 @@ fn impl_descriptors( } FnType::Setter => { let setter_name = - syn::Ident::new(&format!("set_{}", name), Span::call_site()); + syn::Ident::new(&format!("set_{}", name.unraw()), Span::call_site()); quote! { impl #cls { fn #setter_name(&mut self, value: #field_ty) -> pyo3::PyResult<()> { @@ -463,7 +463,7 @@ fn impl_descriptors( .flat_map(|&(ref field, ref fns)| { fns.iter() .map(|desc| { - let name = field.ident.clone().unwrap(); + let name = field.ident.as_ref().unwrap(); // FIXME better doc? let doc = syn::LitStr::new(&name.to_string(), name.span()); @@ -483,8 +483,10 @@ fn impl_descriptors( Ok(impl_py_getter_def(&spec, &impl_wrap_getter(&cls, &spec)?)) } FnType::Setter => { - let setter_name = - syn::Ident::new(&format!("set_{}", name), Span::call_site()); + let setter_name = syn::Ident::new( + &format!("set_{}", name.unraw()), + Span::call_site(), + ); let spec = FnSpec { tp: FnType::Setter, name: &setter_name, diff --git a/tests/test_class_basics.rs b/tests/test_class_basics.rs index 4c340acf..c026bfd1 100644 --- a/tests/test_class_basics.rs +++ b/tests/test_class_basics.rs @@ -69,7 +69,10 @@ fn custom_names() { } #[pyclass] -struct RawIdents {} +struct RawIdents { + #[pyo3(get, set)] + r#type: i64, +} #[pymethods] impl RawIdents { @@ -83,6 +86,7 @@ fn test_raw_idents() { let typeobj = py.get_type::(); py_assert!(py, typeobj, "not hasattr(typeobj, 'r#fn')"); py_assert!(py, typeobj, "hasattr(typeobj, 'fn')"); + py_assert!(py, typeobj, "hasattr(typeobj, 'type')"); } #[pyclass]