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

Use Ident::parse_any for name attributes #4226

Merged
merged 1 commit into from
Jun 3, 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/4226.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes a compile error when declaring a standalone function or class method with a Python name that is a Rust keyword.
3 changes: 2 additions & 1 deletion pyo3-macros-backend/src/attributes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use proc_macro2::TokenStream;
use quote::ToTokens;
use syn::{
ext::IdentExt,
parse::{Parse, ParseStream},
punctuated::Punctuated,
spanned::Spanned,
Expand Down Expand Up @@ -72,7 +73,7 @@ pub struct NameLitStr(pub Ident);
impl Parse for NameLitStr {
fn parse(input: ParseStream<'_>) -> Result<Self> {
let string_literal: LitStr = input.parse()?;
if let Ok(ident) = string_literal.parse() {
if let Ok(ident) = string_literal.parse_with(Ident::parse_any) {
Ok(NameLitStr(ident))
} else {
bail_spanned!(string_literal.span() => "expected a single identifier in double quotes")
Expand Down
30 changes: 30 additions & 0 deletions tests/test_class_basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,36 @@ fn custom_names() {
});
}

#[pyclass(name = "loop")]
struct ClassRustKeywords {
#[pyo3(name = "unsafe", get, set)]
unsafe_variable: usize,
}

#[pymethods]
impl ClassRustKeywords {
#[pyo3(name = "struct")]
fn struct_method(&self) {}

#[staticmethod]
#[pyo3(name = "type")]
fn type_method() {}
}

#[test]
fn keyword_names() {
Python::with_gil(|py| {
let typeobj = py.get_type_bound::<ClassRustKeywords>();
py_assert!(py, typeobj, "typeobj.__name__ == 'loop'");
py_assert!(py, typeobj, "typeobj.struct.__name__ == 'struct'");
py_assert!(py, typeobj, "typeobj.type.__name__ == 'type'");
py_assert!(py, typeobj, "typeobj.unsafe.__name__ == 'unsafe'");
py_assert!(py, typeobj, "not hasattr(typeobj, 'unsafe_variable')");
py_assert!(py, typeobj, "not hasattr(typeobj, 'struct_method')");
py_assert!(py, typeobj, "not hasattr(typeobj, 'type_method')");
});
}

#[pyclass]
struct RawIdents {
#[pyo3(get, set)]
Expand Down
12 changes: 12 additions & 0 deletions tests/test_pyfunction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ use pyo3::types::{self, PyCFunction};
#[path = "../src/tests/common.rs"]
mod common;

#[pyfunction(name = "struct")]
fn struct_function() {}

#[test]
fn test_rust_keyword_name() {
Python::with_gil(|py| {
let f = wrap_pyfunction_bound!(struct_function)(py).unwrap();

py_assert!(py, f, "f.__name__ == 'struct'");
});
}

#[pyfunction(signature = (arg = true))]
fn optional_bool(arg: Option<bool>) -> String {
format!("{:?}", arg)
Expand Down
Loading