Skip to content

Commit

Permalink
derive(FromPyObject): support raw identifiers (#4814)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tpt authored Dec 23, 2024
1 parent ea8c461 commit 54cfaf2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
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);
});
}

0 comments on commit 54cfaf2

Please sign in to comment.