Do not include the double quotes in `#[pyclass(name = "literal")]`

This commit is contained in:
scalexm 2020-11-30 22:04:07 +01:00
parent 92b7a9736c
commit 7dadf59fde
2 changed files with 15 additions and 11 deletions

View File

@ -15,7 +15,7 @@ use syn::{parse_quote, Expr, Token};
/// The parsed arguments of the pyclass macro
pub struct PyClassArgs {
pub freelist: Option<syn::Expr>,
pub name: Option<syn::Expr>,
pub name: Option<syn::ExprPath>,
pub flags: Vec<syn::Expr>,
pub base: syn::TypePath,
pub has_extends: bool,
@ -92,18 +92,17 @@ impl PyClassArgs {
self.freelist = Some(syn::Expr::clone(right));
}
"name" => match &**right {
syn::Expr::Lit(
lit
@
syn::ExprLit {
lit: syn::Lit::Str(..),
..
},
) => {
self.name = Some(lit.clone().into());
syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(lit),
..
}) => {
self.name = match lit.parse() {
Ok(name) => Some(name),
Err(..) => expected!("type name (e.g., Name or \"Name\")"),
}
}
syn::Expr::Path(exp) if exp.path.segments.len() == 1 => {
self.name = Some(exp.clone().into());
self.name = Some(exp.clone());
}
_ => expected!("type name (e.g., Name or \"Name\")"),
},

View File

@ -79,12 +79,17 @@ impl EmptyClass2 {
fn bar_static() {}
}
#[pyclass(name = "CustomName")]
struct EmptyClass3 {}
#[test]
fn custom_names() {
let gil = Python::acquire_gil();
let py = gil.python();
let typeobj = py.get_type::<EmptyClass2>();
let typeobj2 = py.get_type::<EmptyClass3>();
py_assert!(py, typeobj, "typeobj.__name__ == 'CustomName'");
py_assert!(py, typeobj2, "typeobj2.__name__ == 'CustomName'");
py_assert!(py, typeobj, "typeobj.custom_fn.__name__ == 'custom_fn'");
py_assert!(
py,