@@ -361,31 +361,19 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
361
361
}
362
362
if ( argMatchedMethods . Count ( ) > 0 )
363
363
{
364
- // Order matched methods by number of kwargs matched and get the max possible number
365
- // of kwargs matched
366
364
var bestKwargMatchCount = argMatchedMethods . Max ( x=> x . KwargsMatched ) ;
365
+ var fewestDefaultsRequired = argMatchedMethods . Where ( x=> x . KwargsMatched == bestKwargMatchCount ) . Min ( x=> x . DefaultsNeeded ) ;
367
366
368
- List < MatchedMethod > bestKwargMatches = new List < MatchedMethod > ( argMatchedMethods . Count ( ) ) ;
369
- foreach ( MatchedMethod testMatch in argMatchedMethods )
370
- {
371
- if ( testMatch . KwargsMatched == bestKwargMatchCount )
372
- {
373
- bestKwargMatches . Add ( testMatch ) ;
374
- }
375
- }
376
-
377
- // Order by the number of defaults required and find the smallest
378
- var fewestDefaultsRequired = bestKwargMatches . Min ( x=> x . DefaultsNeeded ) ;
379
367
int bestCount = 0 ;
380
368
int bestMatchIndex = - 1 ;
381
369
382
- foreach ( MatchedMethod testMatch in bestKwargMatches )
370
+ foreach ( MatchedMethod testMatch in argMatchedMethods )
383
371
{
384
- if ( testMatch . DefaultsNeeded == fewestDefaultsRequired )
372
+ if ( testMatch . DefaultsNeeded == fewestDefaultsRequired && testMatch . KwargsMatched == bestKwargMatchCount )
385
373
{
386
374
bestCount ++ ;
387
375
if ( bestMatchIndex == - 1 )
388
- bestMatchIndex = bestKwargMatches . IndexOf ( testMatch ) ;
376
+ bestMatchIndex = argMatchedMethods . IndexOf ( testMatch ) ;
389
377
}
390
378
}
391
379
@@ -403,7 +391,7 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
403
391
// in the case of (a) we're done by default. For (b) regardless of which
404
392
// method we choose, all arguments are specified _and_ can be converted
405
393
// from python to C# so picking any will suffice
406
- MatchedMethod bestMatch = bestKwargMatches . ElementAt ( bestMatchIndex ) ;
394
+ MatchedMethod bestMatch = argMatchedMethods . ElementAt ( bestMatchIndex ) ;
407
395
var margs = bestMatch . ManagedArgs ;
408
396
var outs = bestMatch . Outs ;
409
397
var mi = bestMatch . Method ;