From b296137950e992c5fb6498e0d28bea37067d7e66 Mon Sep 17 00:00:00 2001
From: Michael Carlstrom <rmc@carlstrom.com>
Date: Wed, 18 Dec 2024 17:09:27 -0800
Subject: [PATCH] add redeclaration test

---
 tests/test_pytypes.cpp |  5 +++++
 tests/test_pytypes.py  | 12 ++++++++++++
 2 files changed, 17 insertions(+)

diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp
index 761d6cfaa..9380c8bfb 100644
--- a/tests/test_pytypes.cpp
+++ b/tests/test_pytypes.cpp
@@ -1058,7 +1058,12 @@ TEST_SUBMODULE(pytypes, m) {
     instance.def(py::init());
     instance.attr_with_type_hint<float>("y");
 
+    m.def("attr_with_type_hint_float_x", [](py::handle obj) {
+        obj.attr_with_type_hint<float>("x");
+    });
+
     m.attr_with_type_hint<py::typing::Final<int>>("CONST_INT") = 3;
+    
     m.attr("defined___cpp_inline_variables") = true;
 #else
     m.attr("defined___cpp_inline_variables") = false;
diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py
index 24c5f2f0e..4c6ee8da9 100644
--- a/tests/test_pytypes.py
+++ b/tests/test_pytypes.py
@@ -1153,6 +1153,18 @@ def test_class_attribute_types() -> None:
     assert instance1.y != instance2.y
 
 
+@pytest.mark.skipif(
+    not m.defined___cpp_inline_variables,
+    reason="C++17 feature __cpp_inline_variables not available.",
+)
+def test_redeclaration_attr_with_type_hint() -> None:
+    obj = m.Instance()
+    m.attr_with_type_hint_float_x(obj)
+    help(obj)
+    assert get_annotations_helper(obj)["x"] == "float"
+    with pytest.raises(RuntimeError, match=r'^__annotations__\["x"\] was set already\.$'):
+        m.attr_with_type_hint_float_x(obj)
+
 @pytest.mark.skipif(
     not m.defined___cpp_inline_variables,
     reason="C++17 feature __cpp_inline_variables not available.",