Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-Authored-By: Georg Brandl <[email protected]>
  • Loading branch information
kngwyu and birkenfeld committed Mar 23, 2020
1 parent 9b5ea3f commit 3b17ab0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
32 changes: 12 additions & 20 deletions pyo3-derive-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
));
};
}
Expand All @@ -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) => {
Expand All @@ -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 {
Expand All @@ -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(())
Expand All @@ -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",
))
}
};
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/invalid_pyclass_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ struct InvalidName {}
#[pyclass(module = my_module)]
struct InvalidModule {}

#[pyclass(weakrev)]
struct InvalidArg {}

fn main() {}
14 changes: 10 additions & 4 deletions tests/ui/invalid_pyclass_args.stderr
Original file line number Diff line number Diff line change
@@ -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)]
| ^^^^^^^

0 comments on commit 3b17ab0

Please sign in to comment.