From 48cd8fca40425ba4671be89d67012a6b93cdb610 Mon Sep 17 00:00:00 2001 From: "quantconnect-kronos[bot]" <346399058+quantconnect-kronos[bot]@users.noreply.github.com> Date: Mon, 23 Nov 2026 19:02:09 +0000 Subject: [PATCH 1/5] Reach a namespaced parent's Python overrides from a namespaced subclass The generated type of a __namespace__ subclass reflects its own members, such as its overrides of the base's virtual methods, into the Python class dict. Subclassing another __namespace__ class, those members hid the Python overrides the parent defines, so .NET calls fell back to the .NET implementation, or threw NotImplementedException for an abstract member. Drop a reflected member from the new class dict when the Python base class overrides it in Python, so lookups reach the nearest Python override as Python's method resolution does. Closes #172 Co-Authored-By: Claude Opus 5.5 --- src/runtime/Types/ReflectedClrType.cs | 23 +++++++++++++ tests/test_subclass.py | 47 +++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/src/runtime/Types/ReflectedClrType.cs b/src/runtime/Types/ReflectedClrType.cs index 15ea5c2b2..ffc53fef5 100644 --- a/src/runtime/Types/ReflectedClrType.cs +++ b/src/runtime/Types/ReflectedClrType.cs @@ -86,6 +86,29 @@ internal static NewReference CreateSubclass(ClassBase baseClass, // derived class we want the python overrides in there instead if they exist. var cls_dict = Util.ReadRef(py_type, TypeOffset.tp_dict); ThrowIfIsNotZero(Runtime.PyDict_Update(cls_dict, dict)); + + // The generated type's own members, such as the overrides of its base's virtual methods, would + // hide the Python overrides of a Python base class (a __namespace__ subclass of a __namespace__ + // subclass), so let lookups reach those as Python's method resolution does. + var pyBaseType = Util.ReadRef(py_type, TypeOffset.tp_base); + using var clsDict = new PyDict(cls_dict); + using var keys = clsDict.Keys(); + foreach (PyObject key in keys) + { + using (key) + { + var inherited = Runtime._PyType_Lookup(pyBaseType, key.Reference); + if (inherited.IsNull + || ManagedType.GetManagedObject(inherited) != null + || !Runtime.PyDict_GetItem(dict, key.Reference).IsNull + || ManagedType.GetManagedObject(Runtime.PyDict_GetItem(cls_dict, key.Reference)) == null) + { + continue; + } + ThrowIfIsNotZero(Runtime.PyDict_DelItem(cls_dict, key.Reference)); + } + } + // Update the __classcell__ if it exists BorrowedReference cell = Runtime.PyDict_GetItemString(cls_dict, "__classcell__"); if (!cell.IsNull) diff --git a/tests/test_subclass.py b/tests/test_subclass.py index 85c50d21a..b1c0199e8 100644 --- a/tests/test_subclass.py +++ b/tests/test_subclass.py @@ -197,6 +197,53 @@ def test_create_instance(): assert id(y) == id(ob2) +def test_namespace_subclass_of_namespace_subclass(): + """Test managed code reaches the Python overrides a namespaced subclass inherits from a namespaced parent""" + subnamespace = test_namespace_subclass_of_namespace_subclass.__name__ + namespace = "Python.Test." + subnamespace + DerivedClass = derived_class_fixture(subnamespace) + + class ChildClass(DerivedClass): + __namespace__ = namespace + + class GrandChildClass(ChildClass): + __namespace__ = namespace + + def bar(self, x, i): + return "GrandChildClass" + + for ob in (ChildClass(), GrandChildClass(), FunctionsTest.create_instance(GrandChildClass)): + assert ob.foo() == "DerivedClass" + assert FunctionsTest.test_foo(ob) == "DerivedClass" + assert ob.not_overriden() == "not_overriden" + assert list(SubClassTest.test_list(ob)) == ["A", "B", "C"] + + assert FunctionsTest.test_bar(ChildClass(), "bar", 2) == "bar_bar" + assert FunctionsTest.test_bar(GrandChildClass(), "bar", 2) == "GrandChildClass" + + # interface methods have no managed implementation to fall back to + InterfaceTestClass = interface_test_class_fixture(subnamespace) + + class InterfaceChildClass(InterfaceTestClass): + __namespace__ = namespace + + ob = InterfaceChildClass() + assert ob.foo() == "InterfaceTestClass" + assert FunctionsTest.test_foo(ob) == "InterfaceTestClass" + assert FunctionsTest.test_bar(ob, "bar", 2) == "bar/bar" + + class InitParentClass(SubClassTest): + __namespace__ = namespace + + def __init__(self, value): + self.value = value + + class InitChildClass(InitParentClass): + __namespace__ = namespace + + assert InitChildClass(5).value == 5 + + def test_events(): class EventHandler(object): def handler(self, x, args): From 00ded8615052455643cc193beed168da00fe2f4d Mon Sep 17 00:00:00 2001 From: "quantconnect-kronos[bot]" <346399058+quantconnect-kronos[bot]@users.noreply.github.com> Date: Mon, 23 Nov 2026 19:51:38 +0000 Subject: [PATCH 2/5] Only skip the generated forwarders of the base's virtual methods The class dict filter also removed the reflected constructor, so a child inherited its parent's Python __init__, whose super().__init__() then ran the managed constructor a second time, and the snake_case aliases of members the child declares for .NET, which then resolved to the parent's Python members. Remove only the forwarders the generated type adds for the base's virtual methods, which is what hid the parent's Python overrides. Co-Authored-By: Claude Opus 5.5 --- src/runtime/Types/ReflectedClrType.cs | 29 ++++++++++++++--- src/testing/constructortests.cs | 10 ++++++ tests/test_subclass.py | 45 ++++++++++++++++++++++++--- 3 files changed, 74 insertions(+), 10 deletions(-) diff --git a/src/runtime/Types/ReflectedClrType.cs b/src/runtime/Types/ReflectedClrType.cs index ffc53fef5..9025c3c94 100644 --- a/src/runtime/Types/ReflectedClrType.cs +++ b/src/runtime/Types/ReflectedClrType.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Linq; +using System.Reflection; using System.Runtime.Serialization; using static Python.Runtime.PythonException; @@ -87,10 +89,14 @@ internal static NewReference CreateSubclass(ClassBase baseClass, var cls_dict = Util.ReadRef(py_type, TypeOffset.tp_dict); ThrowIfIsNotZero(Runtime.PyDict_Update(cls_dict, dict)); - // The generated type's own members, such as the overrides of its base's virtual methods, would - // hide the Python overrides of a Python base class (a __namespace__ subclass of a __namespace__ - // subclass), so let lookups reach those as Python's method resolution does. + // The generated type's overrides of its base's virtual methods would hide the Python overrides of + // a Python base class (a __namespace__ subclass of a __namespace__ subclass), so let lookups of + // those reach the Python overrides, as Python's method resolution does. var pyBaseType = Util.ReadRef(py_type, TypeOffset.tp_base); + var baseVirtualMethods = baseClass.type.Value.GetMethods() + .Where(m => m.IsVirtual && !m.IsFinal) + .Select(m => m.Name) + .ToHashSet(); using var clsDict = new PyDict(cls_dict); using var keys = clsDict.Keys(); foreach (PyObject key in keys) @@ -101,11 +107,24 @@ internal static NewReference CreateSubclass(ClassBase baseClass, if (inherited.IsNull || ManagedType.GetManagedObject(inherited) != null || !Runtime.PyDict_GetItem(dict, key.Reference).IsNull - || ManagedType.GetManagedObject(Runtime.PyDict_GetItem(cls_dict, key.Reference)) == null) + || ManagedType.GetManagedObject(Runtime.PyDict_GetItem(cls_dict, key.Reference)) is not MethodObject method) { continue; } - ThrowIfIsNotZero(Runtime.PyDict_DelItem(cls_dict, key.Reference)); + + // only the generated forwarders, not the members the class body declares for .NET + var isForwarder = true; + foreach (var member in method.info) + { + isForwarder &= member is MethodInfo { IsVirtual: true, IsFinal: false } + && member.DeclaringType == subType + && baseVirtualMethods.Contains(member.Name) + && Runtime.PyDict_GetItemString(dict, member.Name).IsNull; + } + if (isForwarder) + { + ThrowIfIsNotZero(Runtime.PyDict_DelItem(cls_dict, key.Reference)); + } } } diff --git a/src/testing/constructortests.cs b/src/testing/constructortests.cs index 5912853b0..d1007b150 100644 --- a/src/testing/constructortests.cs +++ b/src/testing/constructortests.cs @@ -108,4 +108,14 @@ public FinalizedConstructorTest(Exception toThrow) Interlocked.Increment(ref Finalized); } } + + public class CountedConstructorTest + { + public static int Calls; + + public CountedConstructorTest() + { + ++Calls; + } + } } diff --git a/tests/test_subclass.py b/tests/test_subclass.py index b1c0199e8..76d2e6037 100644 --- a/tests/test_subclass.py +++ b/tests/test_subclass.py @@ -232,16 +232,51 @@ class InterfaceChildClass(InterfaceTestClass): assert FunctionsTest.test_foo(ob) == "InterfaceTestClass" assert FunctionsTest.test_bar(ob, "bar", 2) == "bar/bar" - class InitParentClass(SubClassTest): + # members the child declares for .NET keep their snake_case aliases over the parent's Python ones + import clr + + class AliasParentClass(SubClassTest): __namespace__ = namespace - def __init__(self, value): - self.value = value + def calculate(self): + return 1 + + @property + def number(self): + return 2 + + class AliasChildClass(AliasParentClass): + __namespace__ = namespace + + @clr.clrmethod(int, []) + def Calculate(self): + return 3 + + @clr.clrproperty(int) + def Number(self): + return 4 + + ob = AliasChildClass() + assert ob.calculate() == 3 + assert ob.Calculate() == 3 + assert ob.number == 4 + assert ob.Number == 4 + + # the managed constructor still runs once + from Python.Test import CountedConstructorTest + + class CountedParentClass(CountedConstructorTest): + __namespace__ = namespace + + def __init__(self): + super().__init__() - class InitChildClass(InitParentClass): + class CountedChildClass(CountedParentClass): __namespace__ = namespace - assert InitChildClass(5).value == 5 + CountedConstructorTest.Calls = 0 + CountedChildClass() + assert CountedConstructorTest.Calls == 1 def test_events(): From 9d63d130091139f0982d6d6cdaa7ad2f38f34ad1 Mon Sep 17 00:00:00 2001 From: "quantconnect-kronos[bot]" <346399058+quantconnect-kronos[bot]@users.noreply.github.com> Date: Mon, 23 Nov 2026 20:23:22 +0000 Subject: [PATCH 3/5] Skip forwarders that share a name with overloads the generated type doesn't override Co-Authored-By: Claude Opus 5.5 --- src/runtime/Types/ReflectedClrType.cs | 14 ++++++++------ tests/test_subclass.py | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/runtime/Types/ReflectedClrType.cs b/src/runtime/Types/ReflectedClrType.cs index 9025c3c94..d81241460 100644 --- a/src/runtime/Types/ReflectedClrType.cs +++ b/src/runtime/Types/ReflectedClrType.cs @@ -112,16 +112,18 @@ internal static NewReference CreateSubclass(ClassBase baseClass, continue; } - // only the generated forwarders, not the members the class body declares for .NET - var isForwarder = true; + // a generated forwarder, possibly among overloads the generated type doesn't override (which + // the parent's Python override replaces too), but not the members the class body declares for .NET + var hasForwarder = false; + var declaredInBody = false; foreach (var member in method.info) { - isForwarder &= member is MethodInfo { IsVirtual: true, IsFinal: false } + hasForwarder |= member is MethodInfo { IsVirtual: true, IsFinal: false } && member.DeclaringType == subType - && baseVirtualMethods.Contains(member.Name) - && Runtime.PyDict_GetItemString(dict, member.Name).IsNull; + && baseVirtualMethods.Contains(member.Name); + declaredInBody |= !Runtime.PyDict_GetItemString(dict, member.Name).IsNull; } - if (isForwarder) + if (hasForwarder && !declaredInBody) { ThrowIfIsNotZero(Runtime.PyDict_DelItem(cls_dict, key.Reference)); } diff --git a/tests/test_subclass.py b/tests/test_subclass.py index 76d2e6037..5205b55c0 100644 --- a/tests/test_subclass.py +++ b/tests/test_subclass.py @@ -232,6 +232,22 @@ class InterfaceChildClass(InterfaceTestClass): assert FunctionsTest.test_foo(ob) == "InterfaceTestClass" assert FunctionsTest.test_bar(ob, "bar", 2) == "bar/bar" + # overloads the generated type doesn't override don't hide the parent's override either + class EqualsParentClass(System.Object): + __namespace__ = namespace + + def Equals(self, other): + return True + + class EqualsChildClass(EqualsParentClass): + __namespace__ = namespace + + other = System.Object() + ob = EqualsChildClass() + assert ob.Equals(other) + assert System.Object.Equals(ob, other) + assert ob.GetType().GetMethod("Equals", [System.Object]).Invoke(ob, [other]) + # members the child declares for .NET keep their snake_case aliases over the parent's Python ones import clr From cba03d4c9e026b816ddc3fd54d5e44be95601fee Mon Sep 17 00:00:00 2001 From: "quantconnect-kronos[bot]" <346399058+quantconnect-kronos[bot]@users.noreply.github.com> Date: Mon, 23 Nov 2026 21:00:47 +0000 Subject: [PATCH 4/5] Keep the accessors of properties the subclass redeclares for .NET Co-Authored-By: Claude Opus 5.5 --- src/runtime/Types/ReflectedClrType.cs | 21 +++++++++++-- tests/test_subclass.py | 45 +++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/runtime/Types/ReflectedClrType.cs b/src/runtime/Types/ReflectedClrType.cs index d81241460..64d54806f 100644 --- a/src/runtime/Types/ReflectedClrType.cs +++ b/src/runtime/Types/ReflectedClrType.cs @@ -113,7 +113,8 @@ internal static NewReference CreateSubclass(ClassBase baseClass, } // a generated forwarder, possibly among overloads the generated type doesn't override (which - // the parent's Python override replaces too), but not the members the class body declares for .NET + // the parent's Python override replaces too), but not the members the class body declares for .NET, + // methods or the accessors of its properties var hasForwarder = false; var declaredInBody = false; foreach (var member in method.info) @@ -121,7 +122,8 @@ internal static NewReference CreateSubclass(ClassBase baseClass, hasForwarder |= member is MethodInfo { IsVirtual: true, IsFinal: false } && member.DeclaringType == subType && baseVirtualMethods.Contains(member.Name); - declaredInBody |= !Runtime.PyDict_GetItemString(dict, member.Name).IsNull; + declaredInBody |= !Runtime.PyDict_GetItemString(dict, member.Name).IsNull + || IsAccessorOfBodyProperty(member, dict); } if (hasForwarder && !declaredInBody) { @@ -146,6 +148,21 @@ internal static NewReference CreateSubclass(ClassBase baseClass, } } + /// + /// Whether the member is an accessor the generated type emits for a property the class body declares + /// with clr.clrproperty, by the names gives them. + /// + static bool IsAccessorOfBodyProperty(MemberInfo member, BorrowedReference dict) + { + if (member is not MethodInfo { IsSpecialName: true } + || !(member.Name.StartsWith("get_") || member.Name.StartsWith("set_"))) + { + return false; + } + var property = Runtime.PyDict_GetItemString(dict, member.Name.Substring(4)); + return !property.IsNull && Runtime.PyObject_HasAttrString(property, "_clr_property_type_") == 1; + } + static ReflectedClrType AllocateClass(Type clrType) { string name = TypeManager.GetPythonTypeName(clrType); diff --git a/tests/test_subclass.py b/tests/test_subclass.py index 5205b55c0..d4487f349 100644 --- a/tests/test_subclass.py +++ b/tests/test_subclass.py @@ -278,6 +278,51 @@ def Number(self): assert ob.number == 4 assert ob.Number == 4 + # nor the accessors of a property the child redeclares + class PropertyParentClass(System.Object): + __namespace__ = namespace + + def get_Number(self): + return 1 + + def set_Number(self, value): + self.value = value + 1 + + Number = clr.clrproperty(int, get_Number, set_Number) + + class PropertyChildClass(PropertyParentClass): + __namespace__ = namespace + + def get_Number(self): + return 3 + + def set_Number(self, value): + self.value = value + 3 + + Number = clr.clrproperty(int, get_Number, set_Number) + + class PropertyDecoratedChildClass(PropertyParentClass): + __namespace__ = namespace + + @clr.clrproperty(int) + def Number(self): + return 3 + + @Number.setter + def Number(self, value): + self.value = value + 3 + + for cls in (PropertyChildClass, PropertyDecoratedChildClass): + ob = cls() + prop = ob.GetType().GetProperty("Number") + assert ob.Number == 3 + assert ob.get_Number() == 3 + assert prop.GetValue(ob) == 3 + ob.set_Number(10) + assert ob.value == 13 + prop.SetValue(ob, 20) + assert ob.value == 23 + # the managed constructor still runs once from Python.Test import CountedConstructorTest From c0b08eff0f88e78dccb0d984bf428158e33ab248 Mon Sep 17 00:00:00 2001 From: "quantconnect-kronos[bot]" <346399058+quantconnect-kronos[bot]@users.noreply.github.com> Date: Mon, 23 Nov 2026 21:26:36 +0000 Subject: [PATCH 5/5] Inline the check for the accessors of properties the class body declares Co-Authored-By: Claude Opus 5.5 --- src/runtime/Types/ReflectedClrType.cs | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/runtime/Types/ReflectedClrType.cs b/src/runtime/Types/ReflectedClrType.cs index 64d54806f..d5b06522c 100644 --- a/src/runtime/Types/ReflectedClrType.cs +++ b/src/runtime/Types/ReflectedClrType.cs @@ -122,8 +122,18 @@ internal static NewReference CreateSubclass(ClassBase baseClass, hasForwarder |= member is MethodInfo { IsVirtual: true, IsFinal: false } && member.DeclaringType == subType && baseVirtualMethods.Contains(member.Name); + + // an accessor the generated type emits for a property the class body declares with + // clr.clrproperty, by the names ClassDerivedObject gives them + var property = member is MethodInfo { IsSpecialName: true } + && (member.Name.StartsWith("get_") || member.Name.StartsWith("set_")) + ? Runtime.PyDict_GetItemString(dict, member.Name.Substring(4)) + : BorrowedReference.Null; + var isBodyPropertyAccessor = !property.IsNull + && Runtime.PyObject_HasAttrString(property, "_clr_property_type_") == 1; + declaredInBody |= !Runtime.PyDict_GetItemString(dict, member.Name).IsNull - || IsAccessorOfBodyProperty(member, dict); + || isBodyPropertyAccessor; } if (hasForwarder && !declaredInBody) { @@ -148,21 +158,6 @@ internal static NewReference CreateSubclass(ClassBase baseClass, } } - /// - /// Whether the member is an accessor the generated type emits for a property the class body declares - /// with clr.clrproperty, by the names gives them. - /// - static bool IsAccessorOfBodyProperty(MemberInfo member, BorrowedReference dict) - { - if (member is not MethodInfo { IsSpecialName: true } - || !(member.Name.StartsWith("get_") || member.Name.StartsWith("set_"))) - { - return false; - } - var property = Runtime.PyDict_GetItemString(dict, member.Name.Substring(4)); - return !property.IsNull && Runtime.PyObject_HasAttrString(property, "_clr_property_type_") == 1; - } - static ReflectedClrType AllocateClass(Type clrType) { string name = TypeManager.GetPythonTypeName(clrType);