From 890fcae7d83b03a224304cc6e878da987a6efebd Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Sun, 15 Dec 2024 16:51:06 -0500 Subject: [PATCH] added instance check --- tests/test_pytypes.cpp | 15 ++++++++++----- tests/test_pytypes.py | 29 +++++++++++++++++++---------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 4bb6d0c251..40faad17c1 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -1045,13 +1045,18 @@ TEST_SUBMODULE(pytypes, m) { struct Empty {}; py::class_(m, "EmptyAnnotationClass"); - struct Point {}; - auto point = py::class_(m, "Point"); - point.def(py::init()); - point.attr_with_type_hint>("x"); - point.attr_with_type_hint>>("dict_str_int") + struct Static {}; + auto static_class = py::class_(m, "Static"); + static_class.def(py::init()); + static_class.attr_with_type_hint>("x") = 1.0; + static_class.attr_with_type_hint>>("dict_str_int") = py::dict(); + struct Instance {}; + auto instance = py::class_(m, "Instance", py::dynamic_attr()); + instance.def(py::init()); + instance.attr_with_type_hint("y"); + m.attr_with_type_hint>("CONST_INT") = 3; m.attr("defined_PYBIND11_CPP17") = true; #else diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 6d6fd717e4..554376ff9f 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1127,21 +1127,30 @@ def test_module_attribute_types() -> None: ) def test_class_attribute_types() -> None: empty_annotations = get_annotations_helper(m.EmptyAnnotationClass) - annotations = get_annotations_helper(m.Point) + static_annotations = get_annotations_helper(m.Static) + instance_annotations = get_annotations_helper(m.Instance) assert empty_annotations is None - assert annotations["x"] == "ClassVar[float]" - assert annotations["dict_str_int"] == "ClassVar[dict[str, int]]" + assert static_annotations["x"] == "ClassVar[float]" + assert static_annotations["dict_str_int"] == "ClassVar[dict[str, int]]" - m.Point.x = 1.0 - assert m.Point.x == 1.0 + assert m.Static.x == 1.0 - m.Point.x = 3.0 - point = m.Point() - assert point.x == 3.0 + m.Static.x = 3.0 + static = m.Static() + assert static.x == 3.0 - point.dict_str_int["hi"] = 3 - assert m.Point().dict_str_int == {"hi": 3} + static.dict_str_int["hi"] = 3 + assert m.Static().dict_str_int == {"hi": 3} + + assert instance_annotations["y"] == "float" + instance1 = m.Instance() + instance1.y = 4.0 + + instance2 = m.Instance() + instance2.y = 5.0 + + assert instance1.y != instance2.y @pytest.mark.skipif(