From d3d68eafb4370e338403fba4b4bcf7d036d3008c Mon Sep 17 00:00:00 2001 From: scalexm Date: Thu, 7 May 2020 20:13:10 +0200 Subject: [PATCH] Add a test with class attrs returning `PyClass` instances --- tests/test_class_attributes.rs | 40 +++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/tests/test_class_attributes.rs b/tests/test_class_attributes.rs index ab679c02..692a6629 100644 --- a/tests/test_class_attributes.rs +++ b/tests/test_class_attributes.rs @@ -3,7 +3,16 @@ use pyo3::prelude::*; mod common; #[pyclass] -struct Foo {} +struct Foo { + #[pyo3(get)] + x: i32, +} + +#[pyclass] +struct Bar { + #[pyo3(get)] + x: i32, +} #[pymethods] impl Foo { @@ -17,6 +26,24 @@ impl Foo { fn b() -> String { "bar".to_string() } + + #[classattr] + fn foo() -> Foo { + Foo { x: 1 } + } + + #[classattr] + fn bar() -> Bar { + Bar { x: 2 } + } +} + +#[pymethods] +impl Bar { + #[classattr] + fn foo() -> Foo { + Foo { x: 3 } + } } #[test] @@ -35,3 +62,14 @@ fn class_attributes_are_immutable() { let foo_obj = py.get_type::(); py_expect_exception!(py, foo_obj, "foo_obj.a = 6", TypeError); } + +#[test] +fn recursive_class_attributes() { + let gil = Python::acquire_gil(); + let py = gil.python(); + let foo_obj = py.get_type::(); + let bar_obj = py.get_type::(); + py_assert!(py, foo_obj, "foo_obj.foo.x == 1"); + py_assert!(py, foo_obj, "foo_obj.bar.x == 2"); + py_assert!(py, bar_obj, "bar_obj.foo.x == 3"); +}