@@ -361,31 +361,19 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
361361 }
362362 if ( argMatchedMethods . Count ( ) > 0 )
363363 {
364- // Order matched methods by number of kwargs matched and get the max possible number
365- // of kwargs matched
366364 var bestKwargMatchCount = argMatchedMethods . Max ( x => x . KwargsMatched ) ;
365+ var fewestDefaultsRequired = argMatchedMethods . Where ( x => x . KwargsMatched == bestKwargMatchCount ) . Min ( x => x . DefaultsNeeded ) ;
367366
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 ) ;
379367 int bestCount = 0 ;
380368 int bestMatchIndex = - 1 ;
381369
382- foreach ( MatchedMethod testMatch in bestKwargMatches )
370+ foreach ( MatchedMethod testMatch in argMatchedMethods )
383371 {
384- if ( testMatch . DefaultsNeeded == fewestDefaultsRequired )
372+ if ( testMatch . DefaultsNeeded == fewestDefaultsRequired && testMatch . KwargsMatched == bestKwargMatchCount )
385373 {
386374 bestCount ++ ;
387375 if ( bestMatchIndex == - 1 )
388- bestMatchIndex = bestKwargMatches . IndexOf ( testMatch ) ;
376+ bestMatchIndex = argMatchedMethods . IndexOf ( testMatch ) ;
389377 }
390378 }
391379
@@ -403,7 +391,7 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
403391 // in the case of (a) we're done by default. For (b) regardless of which
404392 // method we choose, all arguments are specified _and_ can be converted
405393 // from python to C# so picking any will suffice
406- MatchedMethod bestMatch = bestKwargMatches . ElementAt ( bestMatchIndex ) ;
394+ MatchedMethod bestMatch = argMatchedMethods . ElementAt ( bestMatchIndex ) ;
407395 var margs = bestMatch . ManagedArgs ;
408396 var outs = bestMatch . Outs ;
409397 var mi = bestMatch . Method ;
0 commit comments