Skip to content

Commit

Permalink
macros: Use macro-defined name for enums in __repr__
Browse files Browse the repository at this point in the history
  • Loading branch information
yodaldevoid committed Jun 21, 2022
1 parent 53b83cc commit 2122faa
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix FFI definition `PyGetSetDef` to have `*const c_char` for `doc` member (not `*mut c_char`). [#2439](https://github.com/PyO3/pyo3/pull/2439)
- Fix `#[pyo3(from_py_with = "...")]` being ignored for 1-element tuple structs and transparent structs. [#2440](https://github.com/PyO3/pyo3/pull/2440)
- Use `memoffset` for computing PyCell offsets [#2450](https://github.com/PyO3/pyo3/pull/2450)
- Fix incorrect enum names being returned by `repr` for enums renamed by `#[pyclass(name)]` [#2457](https://github.com/PyO3/pyo3/pull/2457)

## [0.16.5] - 2022-05-15

Expand Down
2 changes: 1 addition & 1 deletion pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ fn impl_enum_class(
let variants_repr = variants.iter().map(|variant| {
let variant_name = variant.ident;
// Assuming all variants are unit variants because they are the only type we support.
let repr = format!("{}.{}", cls, variant_name);
let repr = format!("{}.{}", get_class_python_name(cls, args), variant_name);
quote! { #cls::#variant_name => #repr, }
});
let mut repr_impl: syn::ImplItemMethod = syn::parse_quote! {
Expand Down
14 changes: 14 additions & 0 deletions tests/test_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,17 @@ enum TestReprParse {
fn test_repr_parse() {
assert_eq!(std::mem::align_of::<TestReprParse>(), 8);
}

#[pyclass(name = "MyEnum")]
#[derive(Debug, PartialEq, Clone)]
pub enum RenameEnum {
Variant,
}

#[test]
fn test_rename_enum_repr_correct() {
Python::with_gil(|py| {
let var1 = Py::new(py, RenameEnum::Variant).unwrap();
py_assert!(py, var1, "repr(var1) == 'MyEnum.Variant'");
})
}

0 comments on commit 2122faa

Please sign in to comment.