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

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

Merged
merged 2 commits into from
Dec 12, 2020
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Add `PyAny::is_instance()` method. [#1276](https://github.com/PyO3/pyo3/pull/1276)
- Add support for conversion between `char` and `PyString`. [#1282](https://github.com/PyO3/pyo3/pull/1282)
- Add FFI definitions for `PyBuffer_SizeFromFormat`, `PyObject_LengthHint`, `PyObject_CallNoArgs`, `PyObject_CallOneArg`, `PyObject_CallMethodNoArgs`, `PyObject_CallMethodOneArg`, `PyObject_VectorcallDict`, and `PyObject_VectorcallMethod`. [#1287](https://github.com/PyO3/pyo3/pull/1287)
- Allow the use of a string literal in `#[pyclass(name = "string literal")]`. [#1295](https://github.com/PyO3/pyo3/pull/1295)
- Add section about Python::check_signals to the FAQ page of the user guide. [#1301](https://github.com/PyO3/pyo3/pull/1301)

### Changed
Expand All @@ -34,6 +33,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Rename `PyTypeInfo::is_instance` and `PyTypeInfo::is_exact_instance` to `PyTypeInfo::is_type_of` and `PyTypeInfo::is_exact_type_of`. [#1278](https://github.com/PyO3/pyo3/pull/1278)
- Optimize `PyAny::call0`, `Py::call0` and `PyAny::call_method0` and `Py::call_method0` on Python 3.9 and up. [#1287](https://github.com/PyO3/pyo3/pull/1285)
- Deprecate `Python::is_instance`, `Python::is_subclass`, `Python::release`, and `Python::xdecref`. [#1292](https://github.com/PyO3/pyo3/pull/1292)
- Require double-quotes for pyclass name argument e.g `#[pyclass(name = "MyClass")]`. [#1303](https://github.com/PyO3/pyo3/pull/1303)

### Removed
- Remove deprecated ffi definitions `PyUnicode_AsUnicodeCopy`, `PyUnicode_GetMax`, `_Py_CheckRecursionLimit`, `PyObject_AsCharBuffer`, `PyObject_AsReadBuffer`, `PyObject_CheckReadBuffer` and `PyObject_AsWriteBuffer`, which will be removed in Python 3.10. [#1217](https://github.com/PyO3/pyo3/pull/1217)
Expand Down
41 changes: 24 additions & 17 deletions pyo3-derive-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::Ident>,
pub flags: Vec<syn::Expr>,
pub base: syn::TypePath,
pub has_extends: bool,
Expand Down Expand Up @@ -92,20 +92,30 @@ 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 = Some(lit.parse().map_err(|_| {
syn::Error::new_spanned(
lit,
"expected a single identifier in double-quotes",
)
})?);
}
syn::Expr::Path(exp) if exp.path.segments.len() == 1 => {
self.name = Some(exp.clone().into());
return Err(syn::Error::new_spanned(
exp,
format!(
concat!(
"since PyO3 0.13 a pyclass name should be in double-quotes, ",
"e.g. \"{}\""
),
exp.path.get_ident().expect("path has 1 segment")
),
));
}
_ => expected!("type name (e.g., Name or \"Name\")"),
_ => expected!("type name (e.g. \"Name\")"),
},
"extends" => match &**right {
syn::Expr::Path(exp) => {
Expand Down Expand Up @@ -272,11 +282,8 @@ fn impl_proto_inventory(cls: &syn::Ident) -> TokenStream {
}
}

fn get_class_python_name(cls: &syn::Ident, attr: &PyClassArgs) -> TokenStream {
match &attr.name {
Some(name) => quote! { #name },
None => quote! { #cls },
}
fn get_class_python_name<'a>(cls: &'a syn::Ident, attr: &'a PyClassArgs) -> &'a syn::Ident {
attr.name.as_ref().unwrap_or(cls)
}

fn impl_class(
Expand Down
4 changes: 2 additions & 2 deletions pyo3-derive-backend/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ fn parse_text_signature_attr<T: Display + quote::ToTokens + ?Sized>(
}
}

pub fn parse_text_signature_attrs<T: Display + quote::ToTokens + ?Sized>(
pub fn parse_text_signature_attrs(
attrs: &mut Vec<syn::Attribute>,
python_name: &T,
python_name: &syn::Ident,
) -> syn::Result<Option<syn::LitStr>> {
let mut text_signature = None;
let mut attrs_out = Vec::with_capacity(attrs.len());
Expand Down
2 changes: 1 addition & 1 deletion tests/test_class_basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn class_with_docstr() {
}
}

#[pyclass(name=CustomName)]
#[pyclass(name = "CustomName")]
struct EmptyClass2 {}

#[pymethods]
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/invalid_pyclass_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ struct InvalidExtends {}
#[pyclass(name = m::MyClass)]
struct InvalidName {}

#[pyclass(name = "Custom Name")]
struct InvalidName2 {}

#[pyclass(name = CustomName)]
struct DeprecatedName {}

#[pyclass(module = my_module)]
struct InvalidModule {}

Expand Down
22 changes: 17 additions & 5 deletions tests/ui/invalid_pyclass_args.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,32 @@ error: Expected type path (e.g., my_mod::BaseClass)
6 | #[pyclass(extends = "PyDict")]
| ^^^^^^^^

error: Expected type name (e.g., Name or "Name")
error: Expected type name (e.g. "Name")
--> $DIR/invalid_pyclass_args.rs:9:18
|
9 | #[pyclass(name = m::MyClass)]
| ^^^^^^^^^^

error: expected a single identifier in double-quotes
--> $DIR/invalid_pyclass_args.rs:12:18
|
12 | #[pyclass(name = "Custom Name")]
| ^^^^^^^^^^^^^

error: since PyO3 0.13 a pyclass name should be in double-quotes, e.g. "CustomName"
--> $DIR/invalid_pyclass_args.rs:15:18
|
15 | #[pyclass(name = CustomName)]
| ^^^^^^^^^^

error: Expected string literal (e.g., "my_mod")
--> $DIR/invalid_pyclass_args.rs:12:20
--> $DIR/invalid_pyclass_args.rs:18:20
|
12 | #[pyclass(module = my_module)]
18 | #[pyclass(module = my_module)]
| ^^^^^^^^^

error: Expected one of gc/weakref/subclass/dict/unsendable
--> $DIR/invalid_pyclass_args.rs:15:11
--> $DIR/invalid_pyclass_args.rs:21:11
|
15 | #[pyclass(weakrev)]
21 | #[pyclass(weakrev)]
| ^^^^^^^