From 649b439463b9e106fe9b0b8732b77cd995752593 Mon Sep 17 00:00:00 2001 From: scalexm Date: Wed, 25 Nov 2020 21:38:25 +0100 Subject: [PATCH] Allow `#[pyclass(name = "string literal")]` This is also more in line with the `#[name = "..."]` attribute on methods. --- CHANGELOG.md | 1 + pyo3-derive-backend/src/pyclass.rs | 12 +++++++++++- tests/ui/invalid_pyclass_args.stderr | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ddbae8e9..908a17da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ 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) ### Changed - Change return type `PyType::name()` from `Cow` to `PyResult<&str>`. [#1152](https://github.com/PyO3/pyo3/pull/1152) diff --git a/pyo3-derive-backend/src/pyclass.rs b/pyo3-derive-backend/src/pyclass.rs index 09eed257..4497b71e 100644 --- a/pyo3-derive-backend/src/pyclass.rs +++ b/pyo3-derive-backend/src/pyclass.rs @@ -92,10 +92,20 @@ 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::Path(exp) if exp.path.segments.len() == 1 => { self.name = Some(exp.clone().into()); } - _ => expected!("type name (e.g., Name)"), + _ => expected!("type name (e.g., Name or \"Name\")"), }, "extends" => match &**right { syn::Expr::Path(exp) => { diff --git a/tests/ui/invalid_pyclass_args.stderr b/tests/ui/invalid_pyclass_args.stderr index 42b2c460..05de66a4 100644 --- a/tests/ui/invalid_pyclass_args.stderr +++ b/tests/ui/invalid_pyclass_args.stderr @@ -10,7 +10,7 @@ error: Expected type path (e.g., my_mod::BaseClass) 6 | #[pyclass(extends = "PyDict")] | ^^^^^^^^ -error: Expected type name (e.g., Name) +error: Expected type name (e.g., Name or "Name") --> $DIR/invalid_pyclass_args.rs:9:18 | 9 | #[pyclass(name = m::MyClass)]