Skip to content

Commit 33cc7da

Browse files
Keep the default repr for Python subclasses that don't override ToString
The generated type of a Python subclass with __namespace__ overrides every virtual method, ToString() included, so its DeclaringType always passed the check and repr() returned the bare type name. Like PythonDerivedType.InvokeMethod, only count a ToString the Python class defines; otherwise look through the generated types to the .NET base. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 657faff commit 33cc7da

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

‎src/runtime/Types/ClassBase.cs‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,25 @@ public static NewReference tp_repr(BorrowedReference ob)
377377

378378
// show an overridden ToString() so containers of .NET objects print readably: [SPY, AIG].
379379
// ValueType.ToString() only returns the type name, so structs without their own keep the default
380-
var toStringType = instType.GetMethod("ToString", Type.EmptyTypes)?.DeclaringType;
380+
var toStringOwner = instType;
381+
if (co.inst is IPythonDerivedType)
382+
{
383+
// the generated type of a Python subclass overrides every virtual method, so like
384+
// PythonDerivedType.InvokeMethod only count a ToString the Python class defines
385+
using var pyToString = Runtime.PyObject_GetAttrString(ob, "ToString");
386+
if (pyToString.IsNull())
387+
{
388+
Exceptions.Clear();
389+
}
390+
if (pyToString.IsNull() || GetManagedObject(pyToString.Borrow()) is not null)
391+
{
392+
while (typeof(IPythonDerivedType).IsAssignableFrom(toStringOwner))
393+
{
394+
toStringOwner = toStringOwner.BaseType;
395+
}
396+
}
397+
}
398+
var toStringType = toStringOwner.GetMethod("ToString", Type.EmptyTypes)?.DeclaringType;
381399
if (toStringType != typeof(object) && toStringType != typeof(ValueType))
382400
{
383401
string? text = null;

‎tests/test_repr.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ def test_struct_without_tostring_override():
4444
assert repr(ob).startswith("<System.Collections.Generic.List")
4545
assert " object at " in repr(ob)
4646

47+
def test_namespace_subclass():
48+
"""Test Python subclasses with a __namespace__ only show a ToString() that is actually overridden"""
49+
class ReprPlainNs(System.Object):
50+
__namespace__ = "ReprTestNs"
51+
52+
class ReprBarNs(ReprTest.Bar):
53+
__namespace__ = "ReprTestNs"
54+
55+
class ReprOverrideNs(System.Object):
56+
__namespace__ = "ReprTestNs"
57+
58+
def ToString(self):
59+
return "I implement ToString() in Python!"
60+
61+
assert " object at " in repr(ReprPlainNs())
62+
assert repr(ReprBarNs()) == "I implement ToString() but not __repr__()!"
63+
assert repr(ReprOverrideNs()) == "I implement ToString() in Python!"
64+
4765
def test_no_tostring_override():
4866
"""Test object not overriding ToString() keeps the default repr"""
4967
ob = System.Object()

0 commit comments

Comments
 (0)