Skip to content

Commit 24a4308

Browse files
committed
Cleanup
1 parent 4b9d7f4 commit 24a4308

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

src/embed_tests/TestMethodBinder.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -757,18 +757,30 @@ public string Method1(decimal namedArg1 = 1.2m, int namedArg2 = 123)
757757

758758
// ----
759759

760-
public string Method2(string arg1, int arg2, float arg3, float kwarg1 = 1.1f, bool kwarg2 = false, string kwarg3 = "")
760+
public string Method2(string arg1, int arg2, decimal arg3, decimal kwarg1 = 1.1m, bool kwarg2 = false, string kwarg3 = "")
761761
{
762762
return "Method2 Overload 1";
763763
}
764764

765-
public string Method2(string arg1, int arg2, float kwarg1 = 1.1f, bool kwarg2 = false, string kwarg3 = "")
765+
public string Method2(string arg1, int arg2, decimal kwarg1 = 1.1m, bool kwarg2 = false, string kwarg3 = "")
766766
{
767767
return "Method2 Overload 2";
768768
}
769769

770770
// ----
771771

772+
public string Method3(string arg1, int arg2, float arg3, float kwarg1 = 1.1f, bool kwarg2 = false, string kwarg3 = "")
773+
{
774+
return "Method3 Overload 1";
775+
}
776+
777+
public string Method3(string arg1, int arg2, float kwarg1 = 1.1f, bool kwarg2 = false, string kwarg3 = "")
778+
{
779+
return "Method3 Overload 2";
780+
}
781+
782+
// ----
783+
772784
public string ImplicitConversionSameArgumentCount(string symbol, int quantity, float trailingAmount, bool trailingAsPercentage, string tag = "")
773785
{
774786
return "ImplicitConversionSameArgumentCount 1";
@@ -795,8 +807,11 @@ public string ImplicitConversionSameArgumentCount2(string symbol, decimal quanti
795807
}
796808
}
797809

798-
[TestCase("Method1('abc', namedArg1=1.234, namedArg2=321)", "Method1 Overload 1")]
799-
[TestCase("Method2(\"SPY\", 10, 123.4, kwarg1=0.0025, kwarg2=True)", "Method2 Overload 1")]
810+
[TestCase("Method1('abc', namedArg1=10, namedArg2=321)", "Method1 Overload 1")]
811+
[TestCase("Method1('abc', namedArg1=12.34, namedArg2=321)", "Method1 Overload 1")]
812+
[TestCase("Method2(\"SPY\", 10, 123, kwarg1=1, kwarg2=True)", "Method2 Overload 1")]
813+
[TestCase("Method2(\"SPY\", 10, 123.34, kwarg1=1.23, kwarg2=True)", "Method2 Overload 1")]
814+
[TestCase("Method3(\"SPY\", 10, 123.34, kwarg1=1.23, kwarg2=True)", "Method3 Overload 1")]
800815
public void SelectsRightOverloadWithNamedParameters(string methodCallCode, string expectedResult)
801816
{
802817
using var _ = Py.GIL();

src/runtime/MethodBinder.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,6 @@ internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedRe
431431

432432
internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase info)
433433
{
434-
// Relevant function variables used post conversion
435-
Binding bindingUsingImplicitConversion = null;
436-
Binding genericBinding = null;
437-
438434
// If we have KWArgs create dictionary and collect them
439435
Dictionary<string, PyObject> kwArgDict = null;
440436
if (kw != null)
@@ -456,8 +452,8 @@ internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedRe
456452
var methods = info == null ? GetMethods()
457453
: new List<MethodInformation>(1) { new MethodInformation(info, true) };
458454

459-
var matches = new List<MatchedMethod>();
460-
var matchesUsingImplicitConversion = new List<MatchedMethod>();
455+
var matches = new List<MatchedMethod>(methods.Count);
456+
List<MatchedMethod> matchesUsingImplicitConversion = null;
461457

462458
for (var i = 0; i < methods.Count; i++)
463459
{
@@ -517,11 +513,11 @@ internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedRe
517513
var parameter = pi[paramIndex]; // Clr parameter we are targeting
518514
object arg; // Python -> Clr argument
519515

520-
var hasNamedParam = kwArgDict == null ? false : kwArgDict.TryGetValue(paramNames[paramIndex], out tempPyObject);
521-
522516
// Check positional arguments first and then check for named arguments and optional values
523517
if (paramIndex >= pyArgCount)
524518
{
519+
var hasNamedParam = kwArgDict == null ? false : kwArgDict.TryGetValue(paramNames[paramIndex], out tempPyObject);
520+
525521
// All positional arguments have been used:
526522
// Check our KWargs for this parameter
527523
if (hasNamedParam)
@@ -698,18 +694,26 @@ internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedRe
698694
}
699695

700696
var match = new MatchedMethod(kwargsMatched, margs, outs, mi);
701-
if (usedImplicitConversion)
697+
// Only add matches using implicit conversion if no other regular matches were found,
698+
// since we favor regular matches over matches using implicit conversion
699+
if (usedImplicitConversion && matches.Count == 0)
702700
{
701+
if (matchesUsingImplicitConversion == null)
702+
{
703+
matchesUsingImplicitConversion = new List<MatchedMethod>();
704+
}
703705
matchesUsingImplicitConversion.Add(match);
704706
}
705707
else
706708
{
707709
matches.Add(match);
710+
// We don't need the matches using implicit conversion anymore
711+
matchesUsingImplicitConversion = null;
708712
}
709713
}
710714
}
711715

712-
if (matches.Count > 0 || matchesUsingImplicitConversion.Count > 0)
716+
if (matches.Count > 0 || (matchesUsingImplicitConversion != null && matchesUsingImplicitConversion.Count > 0))
713717
{
714718
// We favor matches that do not use implicit conversion
715719
var matchesTouse = matches.Count > 0 ? matches : matchesUsingImplicitConversion;

0 commit comments

Comments
 (0)