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

derive(FromPyObject): support raw identifiers #4814

Merged
merged 1 commit into from
Dec 23, 2024
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/4814.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`derive(FromPyObject)` support raw identifiers like `r#box`.
9 changes: 5 additions & 4 deletions pyo3-macros-backend/src/frompyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::utils::Ctx;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::{
ext::IdentExt,
parenthesized,
parse::{Parse, ParseStream},
parse_quote,
Expand Down Expand Up @@ -323,11 +324,11 @@ impl<'a> Container<'a> {
fn build_struct(&self, struct_fields: &[NamedStructField<'_>], ctx: &Ctx) -> TokenStream {
let Ctx { pyo3_path, .. } = ctx;
let self_ty = &self.path;
let struct_name = &self.name();
let mut fields: Punctuated<TokenStream, syn::Token![,]> = Punctuated::new();
let struct_name = self.name();
let mut fields: Punctuated<TokenStream, Token![,]> = Punctuated::new();
for field in struct_fields {
let ident = &field.ident;
let field_name = ident.to_string();
let ident = field.ident;
let field_name = ident.unraw().to_string();
let getter = match field.getter.as_ref().unwrap_or(&FieldGetter::GetAttr(None)) {
FieldGetter::GetAttr(Some(name)) => {
quote!(#pyo3_path::types::PyAnyMethods::getattr(obj, #pyo3_path::intern!(obj.py(), #name)))
Expand Down
38 changes: 38 additions & 0 deletions tests/test_frompyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,3 +648,41 @@ fn test_transparent_from_py_with() {
assert_eq!(result, expected);
});
}

#[derive(Debug, FromPyObject, PartialEq, Eq)]
pub struct WithKeywordAttr {
r#box: usize,
}

#[pyclass]
pub struct WithKeywordAttrC {
#[pyo3(get)]
r#box: usize,
}

#[test]
fn test_with_keyword_attr() {
Python::with_gil(|py| {
let cls = WithKeywordAttrC { r#box: 3 }.into_pyobject(py).unwrap();
let result = cls.extract::<WithKeywordAttr>().unwrap();
let expected = WithKeywordAttr { r#box: 3 };
assert_eq!(result, expected);
});
}

#[derive(Debug, FromPyObject, PartialEq, Eq)]
pub struct WithKeywordItem {
#[pyo3(item)]
r#box: usize,
}

#[test]
fn test_with_keyword_item() {
Python::with_gil(|py| {
let dict = PyDict::new(py);
dict.set_item("box", 3).unwrap();
let result = dict.extract::<WithKeywordItem>().unwrap();
let expected = WithKeywordItem { r#box: 3 };
assert_eq!(result, expected);
});
}
Loading