Apply suggestions from code review

Co-Authored-By: Georg Brandl <georg@python.org>
This commit is contained in:
Yuji Kanagawa 2020-03-22 18:48:22 +09:00 committed by kngwyu
parent 9b5ea3f46b
commit 3b17ab0529
3 changed files with 25 additions and 24 deletions

View File

@ -74,12 +74,12 @@ impl PyClassArgs {
macro_rules! expected {
($expected: literal) => {
expected!($expected, right)
};
($expected: literal, $span: ident) => {
return Err(syn::Error::new_spanned(
right,
format!(
concat!("Expected ", $expected, ", but got {}"),
quote! { #right }
),
$span,
concat!("Expected ", $expected),
));
};
}
@ -93,7 +93,7 @@ impl PyClassArgs {
syn::Expr::Path(exp) if exp.path.segments.len() == 1 => {
self.name = Some(exp.clone().into());
}
_ => expected!("single type path(e.g., Name)"),
_ => expected!("type name (e.g., Name)"),
},
"extends" => match &**right {
syn::Expr::Path(exp) => {
@ -103,7 +103,7 @@ impl PyClassArgs {
};
self.has_extends = true;
}
_ => expected!("type path(e.g., my_mod::MyClass)"),
_ => expected!("type path (e.g., my_mod::BaseClass)"),
},
"module" => match &**right {
syn::Expr::Lit(syn::ExprLit {
@ -112,17 +112,9 @@ impl PyClassArgs {
}) => {
self.module = Some(lit.clone());
}
_ => expected!(r#"string literal(e.g., "mymod")"#),
_ => expected!(r#"string literal (e.g., "my_mod")"#),
},
x => {
return Err(syn::Error::new_spanned(
left,
format!(
"Expected one of freelist/name/extends/module, but got {}",
x
),
));
}
_ => expected!("one of freelist/name/extends/module", left),
};
Ok(())
@ -144,10 +136,10 @@ impl PyClassArgs {
"dict" => {
parse_quote! {pyo3::type_flags::DICT}
}
x => {
_ => {
return Err(syn::Error::new_spanned(
exp.path.clone(),
format!("Expected one of gc/weakref/subclass/dict, but got {}", x),
&exp.path,
"Expected one of gc/weakref/subclass/dict",
))
}
};

View File

@ -12,4 +12,7 @@ struct InvalidName {}
#[pyclass(module = my_module)]
struct InvalidModule {}
#[pyclass(weakrev)]
struct InvalidArg {}
fn main() {}

View File

@ -1,23 +1,29 @@
error: Expected one of freelist/name/extends/module, but got extend
error: Expected one of freelist/name/extends/module
--> $DIR/invalid_pyclass_args.rs:3:11
|
3 | #[pyclass(extend=pyo3::types::PyDict)]
| ^^^^^^
error: Expected type path(e.g., my_mod::MyClass), but got "PyDict"
error: Expected type path (e.g., my_mod::BaseClass)
--> $DIR/invalid_pyclass_args.rs:6:21
|
6 | #[pyclass(extends = "PyDict")]
| ^^^^^^^^
error: Expected single type path(e.g., Name), but got m :: MyClass
error: Expected type name (e.g., Name)
--> $DIR/invalid_pyclass_args.rs:9:18
|
9 | #[pyclass(name = m::MyClass)]
| ^^^^^^^^^^
error: Expected string literal(e.g., "mymod"), but got my_module
error: Expected string literal (e.g., "my_mod")
--> $DIR/invalid_pyclass_args.rs:12:20
|
12 | #[pyclass(module = my_module)]
| ^^^^^^^^^
error: Expected one of gc/weakref/subclass/dict
--> $DIR/invalid_pyclass_args.rs:15:11
|
15 | #[pyclass(weakrev)]
| ^^^^^^^