diff --git a/src/nb_type.cpp b/src/nb_type.cpp index b39791dc..a536416a 100644 --- a/src/nb_type.cpp +++ b/src/nb_type.cpp @@ -513,7 +513,9 @@ int nb_type_setattro(PyObject* obj, PyObject* name, PyObject* value) { if (cur) { PyTypeObject *tp = int_p->nb_static_property; - if (Py_TYPE(cur) == tp) { + // For type.static_prop = value, call the setter. + // For type.static_prop = another_static_prop, replace the descriptor. + if (Py_TYPE(cur) == tp && Py_TYPE(value) != tp) { int rv = int_p->nb_static_property_descr_set(cur, obj, value); Py_DECREF(cur); return rv; diff --git a/tests/test_classes.cpp b/tests/test_classes.cpp index bf3fd7c7..1bd8648c 100644 --- a/tests/test_classes.cpp +++ b/tests/test_classes.cpp @@ -705,4 +705,12 @@ NB_MODULE(test_classes_ext, m) { nb::inst_mark_ready(h); }) .def_rw("value", &MonkeyPatchable::value); + + struct StaticPropertyOverride {}; + struct StaticPropertyOverride2 : public StaticPropertyOverride {}; + + nb::class_(m, "StaticPropertyOverride") + .def_prop_ro_static("x", [](nb::handle /*unused*/) { return 42; }); + nb::class_(m, "StaticPropertyOverride2") + .def_prop_ro_static("x", [](nb::handle /*unused*/) { return 43; }); } diff --git a/tests/test_classes.py b/tests/test_classes.py index cdf1939c..79eff558 100644 --- a/tests/test_classes.py +++ b/tests/test_classes.py @@ -916,3 +916,7 @@ def my_init(self): t.MonkeyPatchable.__init__ = my_init q = t.MonkeyPatchable() assert q.value == 456 + +def test49_static_property_override(): + assert t.StaticPropertyOverride.x == 42 + assert t.StaticPropertyOverride2.x == 43 diff --git a/tests/test_classes_ext.pyi.ref b/tests/test_classes_ext.pyi.ref index da24759f..ba87bef8 100644 --- a/tests/test_classes_ext.pyi.ref +++ b/tests/test_classes_ext.pyi.ref @@ -180,6 +180,14 @@ class StaticProperties: class StaticProperties2(StaticProperties): pass +class StaticPropertyOverride: + x: int = ... + """(arg: object, /) -> int""" + +class StaticPropertyOverride2(StaticPropertyOverride): + x: int = ... + """(arg: object, /) -> int""" + class Struct: """Some documentation"""