Skip to content

Commit 5231a08

Browse files
committed
Fix non generic method overload resolution
1 parent 52f13ad commit 5231a08

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/embed_tests/TestMethodBinder.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,31 @@ def call_method_with_enum():
14021402
Assert.AreEqual(DayOfWeek.Monday, CSharpModel.ProvidedArgument);
14031403
}
14041404

1405+
[TestCase("call_non_generic_method", "GenericOverloadTestMethod")]
1406+
[TestCase("call_generic_method", "GenericOverloadTestMethod<T>")]
1407+
public void ResolvesToGenericOnlyWhenExplicitlyCalled(string pythonFuncToCall, string expectedMethodCalled)
1408+
{
1409+
using var _ = Py.GIL();
1410+
1411+
var module = PyModule.FromString($"ResolvesToGenericOnlyWhenExplicitlyCalled_{pythonFuncToCall}", @$"
1412+
from clr import AddReference
1413+
AddReference(""System"")
1414+
from Python.EmbeddingTest import *
1415+
1416+
def call_non_generic_method():
1417+
return TestMethodBinder.CSharpModel.GenericOverloadTestMethod(TestMethodBinder.CSharpModel(), 'Test')
1418+
1419+
def call_generic_method():
1420+
return TestMethodBinder.CSharpModel.GenericOverloadTestMethod[TestMethodBinder.CSharpModel](TestMethodBinder.CSharpModel(), 'Test')
1421+
");
1422+
1423+
Assert.DoesNotThrow(() =>
1424+
{
1425+
using var result = module.GetAttr(pythonFuncToCall).Invoke();
1426+
});
1427+
Assert.AreEqual(expectedMethodCalled, CSharpModel.LastFuncCalled);
1428+
}
1429+
14051430
// Used to test that we match this function with Py DateTime & Date Objects
14061431
public static int GetMonth(DateTime test)
14071432
{
@@ -1636,6 +1661,18 @@ public static void TestAction3(CSharpModel model1, CSharpModel model2)
16361661
}
16371662
LastFuncCalled = "TestAction3";
16381663
}
1664+
1665+
public static string GenericOverloadTestMethod(CSharpModel testArg1, string testArg2, decimal testArgs3 = 0m)
1666+
{
1667+
LastFuncCalled = "GenericOverloadTestMethod";
1668+
return string.Empty;
1669+
}
1670+
1671+
public static T GenericOverloadTestMethod<T>(CSharpModel testArg1, string testArg2, decimal testArgs3 = 0m)
1672+
{
1673+
LastFuncCalled = "GenericOverloadTestMethod<T>";
1674+
return default;
1675+
}
16391676
}
16401677

16411678
public class TestImplicitConversion

src/runtime/MethodBinder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,8 @@ internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedRe
780780
else
781781
{
782782
bestMatch = matchesTouse
783+
.GroupBy(x => x.Method.IsGenericMethod)
784+
.MinBy(x => x.Key)
783785
.GroupBy(x => x.KwargsMatched)
784786
.OrderByDescending(x => x.Key)
785787
.First()

0 commit comments

Comments
 (0)