Merge pull request #1507 from scalexm/getter-name

Allow `#[name]` with `#[getter]` and `#[setter]`
This commit is contained in:
Yuji Kanagawa 2021-03-20 16:23:07 +09:00 committed by GitHub
commit f0763f069e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 5 deletions

View file

@ -468,7 +468,16 @@ fn parse_method_attributes(
*attrs = new_attrs;
let python_name = if allow_custom_name {
parse_method_name_attribute(ty.as_ref(), attrs, property_name)?
match parse_method_name_attribute(ty.as_ref(), attrs)? {
Some(python_name) if property_name.is_some() => {
return Err(syn::Error::new_spanned(
python_name,
"name cannot be specified twice",
));
}
Some(python_name) => Some(python_name),
None => property_name,
}
} else {
property_name
};
@ -483,14 +492,13 @@ fn parse_method_attributes(
fn parse_method_name_attribute(
ty: Option<&MethodTypeAttribute>,
attrs: &mut Vec<syn::Attribute>,
property_name: Option<syn::Ident>,
) -> syn::Result<Option<syn::Ident>> {
use MethodTypeAttribute::*;
let name = parse_name_attribute(attrs)?;
// Reject some invalid combinations
if let (Some(name), Some(ty)) = (&name, ty) {
if let New | Call | Getter | Setter = ty {
if let New | Call = ty {
bail_spanned!(name.span() => "name not allowed with this method type");
}
}
@ -499,7 +507,6 @@ fn parse_method_name_attribute(
Ok(match ty {
Some(New) => Some(syn::Ident::new("__new__", proc_macro2::Span::call_site())),
Some(Call) => Some(syn::Ident::new("__call__", proc_macro2::Span::call_site())),
Some(Getter) | Some(Setter) => property_name,
_ => name,
})
}

View file

@ -92,6 +92,12 @@ impl EmptyClass2 {
#[staticmethod]
#[name = "custom_static"]
fn bar_static() {}
#[getter]
#[name = "custom_getter"]
fn foo(&self) -> i32 {
5
}
}
#[test]
@ -106,8 +112,14 @@ fn custom_names() {
typeobj,
"typeobj.custom_static.__name__ == 'custom_static'"
);
py_assert!(
py,
typeobj,
"typeobj.custom_getter.__name__ == 'custom_getter'"
);
py_assert!(py, typeobj, "not hasattr(typeobj, 'bar')");
py_assert!(py, typeobj, "not hasattr(typeobj, 'bar_static')");
py_assert!(py, typeobj, "not hasattr(typeobj, 'foo')");
}
#[pyclass]

View file

@ -1,4 +1,4 @@
error: name not allowed with this method type
error: name cannot be specified twice
--> $DIR/invalid_pymethod_names.rs:10:5
|
10 | #[name = "num"]