Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/embed_tests/TestMethodBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,35 @@ def call_method_with_enum():
Assert.AreEqual(DayOfWeek.Monday, CSharpModel.ProvidedArgument);
}

[TestCase("call_non_generic_method", "GenericOverloadTestMethod")]
[TestCase("call_generic_method", "GenericOverloadTestMethod<T>")]
[TestCase("call_generic_class_method", "GenericOverloadTestClass<T>.GenericOverloadTestMethod")]
public void ResolvesToGenericOnlyWhenExplicitlyCalled(string pythonFuncToCall, string expectedMethodCalled)
{
using var _ = Py.GIL();

var module = PyModule.FromString($"ResolvesToGenericOnlyWhenExplicitlyCalled_{pythonFuncToCall}", @$"
from clr import AddReference
AddReference(""System"")
from Python.EmbeddingTest import *

def call_non_generic_method():
return TestMethodBinder.CSharpModel.GenericOverloadTestMethod(TestMethodBinder.CSharpModel(), 'Test')

def call_generic_method():
return TestMethodBinder.CSharpModel.GenericOverloadTestMethod[TestMethodBinder.CSharpModel](TestMethodBinder.CSharpModel(), 'Test')

def call_generic_class_method():
return GenericOverloadTestClass[TestMethodBinder.CSharpModel].GenericOverloadTestMethod(TestMethodBinder.CSharpModel(), 'Test')
");

Assert.DoesNotThrow(() =>
{
using var result = module.GetAttr(pythonFuncToCall).Invoke();
});
Assert.AreEqual(expectedMethodCalled, CSharpModel.LastFuncCalled);
}

// Used to test that we match this function with Py DateTime & Date Objects
public static int GetMonth(DateTime test)
{
Expand Down Expand Up @@ -1636,6 +1665,18 @@ public static void TestAction3(CSharpModel model1, CSharpModel model2)
}
LastFuncCalled = "TestAction3";
}

public static string GenericOverloadTestMethod(CSharpModel testArg1, string testArg2, decimal testArgs3 = 0m)
{
LastFuncCalled = "GenericOverloadTestMethod";
return string.Empty;
}

public static T GenericOverloadTestMethod<T>(CSharpModel testArg1, string testArg2, decimal testArgs3 = 0m)
{
LastFuncCalled = "GenericOverloadTestMethod<T>";
return default;
}
}

public class TestImplicitConversion
Expand Down Expand Up @@ -1784,4 +1825,14 @@ public enum SomeEnu
B = 2,
}
}

public class GenericOverloadTestClass<T>
{
public static T GenericOverloadTestMethod(T testArg1, string testArg2, decimal testArgs3 = 0m)
{
TestMethodBinder.CSharpModel.LastFuncCalled = "GenericOverloadTestClass<T>.GenericOverloadTestMethod";
return default;

}
}
}
12 changes: 5 additions & 7 deletions src/runtime/MethodBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -780,13 +780,11 @@ internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedRe
else
{
bestMatch = matchesTouse
.GroupBy(x => x.KwargsMatched)
.OrderByDescending(x => x.Key)
.First()
.GroupBy(x => x.ImplicitOperations)
.OrderBy(x => x.Key)
.First()
.MinBy(x => GetMatchedArgumentsPrecedence(x.MethodInformation, pyArgCount, kwArgDict?.Keys));
.OrderBy(x => x.Method.IsGenericMethod)
.ThenByDescending(x => x.KwargsMatched)
.ThenBy(x => x.ImplicitOperations)
.ThenBy(x => GetMatchedArgumentsPrecedence(x.MethodInformation, pyArgCount, kwArgDict?.Keys))
.First();
}

var margs = bestMatch.ManagedArgs;
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
[assembly: InternalsVisibleTo("Python.EmbeddingTest, PublicKey=00240000048000009400000006020000002400005253413100040000110000005ffd8f49fb44ab0641b3fd8d55e749f716e6dd901032295db641eb98ee46063cbe0d4a1d121ef0bc2af95f8a7438d7a80a3531316e6b75c2dae92fb05a99f03bf7e0c03980e1c3cfb74ba690aca2f3339ef329313bcc5dccced125a4ffdc4531dcef914602cd5878dc5fbb4d4c73ddfbc133f840231343e013762884d6143189")]
[assembly: InternalsVisibleTo("Python.Test, PublicKey=00240000048000009400000006020000002400005253413100040000110000005ffd8f49fb44ab0641b3fd8d55e749f716e6dd901032295db641eb98ee46063cbe0d4a1d121ef0bc2af95f8a7438d7a80a3531316e6b75c2dae92fb05a99f03bf7e0c03980e1c3cfb74ba690aca2f3339ef329313bcc5dccced125a4ffdc4531dcef914602cd5878dc5fbb4d4c73ddfbc133f840231343e013762884d6143189")]

[assembly: AssemblyVersion("2.0.52")]
[assembly: AssemblyFileVersion("2.0.52")]
[assembly: AssemblyVersion("2.0.53")]
[assembly: AssemblyFileVersion("2.0.53")]
2 changes: 1 addition & 1 deletion src/runtime/Python.Runtime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<RootNamespace>Python.Runtime</RootNamespace>
<AssemblyName>Python.Runtime</AssemblyName>
<PackageId>QuantConnect.pythonnet</PackageId>
<Version>2.0.52</Version>
<Version>2.0.53</Version>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<RepositoryUrl>https://github.com/pythonnet/pythonnet</RepositoryUrl>
Expand Down
Loading