Skip to content

Commit

Permalink
Merge branch 'pr/tested/1-newtypes' into pr/tested/1-newtypes_dunder-…
Browse files Browse the repository at this point in the history
…overloads
  • Loading branch information
eirannejad committed Apr 2, 2024
2 parents 112f8d6 + aa0f5b3 commit 2a32944
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/runtime/Types/ClassDerived.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,20 @@ internal static Type CreateDerivedType(string typeName,
((m.Name.StartsWith("get_") || m.Name.StartsWith("set_"))
&& pyProperties.Contains(m.Name.Substring(4)));

return !alreadyOverriden
return m.Attributes.HasFlag(MethodAttributes.Virtual)
&& !alreadyOverriden
&& !m.IsPrivate
&& !m.IsAssembly
&& m.Attributes.HasFlag(MethodAttributes.Virtual)
&& !m.Attributes.HasFlag(MethodAttributes.Final)
// overriding generic virtual methods is not supported
// so a call to that should be deferred to the base class method.
&& !m.IsGenericMethod
&& !(IsMethod<OriginalMethod>(m) || IsMethod<RedirectedMethod>(m));
// FIXME:
// crude way of filtering out assembly-internal methods
// 'private protected'
// 'internal override'
&& !m.IsFamilyAndAssembly
// overriding generic virtual methods is not supported
// so a call to that should be deferred to the base class method.
&& !m.IsGenericMethod
&& !m.Attributes.HasFlag(MethodAttributes.Final)
&& !(IsMethod<OriginalMethod>(m) || IsMethod<RedirectedMethod>(m));
})
.Concat(baseInterfaces.SelectMany(x => x.GetMethods()))
.ToList();
Expand Down
13 changes: 13 additions & 0 deletions src/testing/classtest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,17 @@ public static void TestObject(object obj)
}
}
}

public class ClassWithInternalBase
{
private protected virtual bool ThisShouldBeAccessibleFromPythonDerived() => false;
}

public class ClassWithInternal : ClassWithInternalBase
{
private protected override bool ThisShouldBeAccessibleFromPythonDerived()
{
return base.ThisShouldBeAccessibleFromPythonDerived();
}
}
}
12 changes: 12 additions & 0 deletions tests/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,15 @@ def foo():

assert callable(System.String("foo")) == False
assert callable(System.Action(foo)) == True


def test_derive_class_with_internal_virutal_method():
"""Test subclassing classes that have internal override methods work"""

# this class definition should succeed
class MyClass(Test.ClassWithInternal):
def __init__(self):
pass

MyClass()

0 comments on commit 2a32944

Please sign in to comment.