Fix IndexOutOfRangeException on empty **kwargs call to overloaded method#133
Merged
jhonabreul merged 2 commits intoJul 9, 2026
Conversation
…hod (QuantConnect#132) Calling an overloaded method with an empty kwargs mapping (e.g. obj.Method(arg, **{}), common when forwarding *args/**kwargs from a wrapper) crashed with an unhandled IndexOutOfRangeException in MethodBinder.CheckMethodArgumentsMatch. Since 10e721b (PR QuantConnect#83), the parameter names array is only populated when there are named arguments, but the kwargs code paths only checked the kwargs dictionary for null, so a non-null empty dict indexed into an empty names array. Treat an empty kwargs dict as no keyword arguments, matching Python semantics where f(x, **{}) is equivalent to f(x). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Martin-Molinero
approved these changes
Jul 9, 2026
Bump package <Version>, AssemblyVersion/AssemblyFileVersion and the perf-test baseline reference to 2.0.60. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Calling an overloaded .NET method from Python with an empty
**kwargsmapping (e.g.obj.Method(arg, **{}), common when forwarding*args/**kwargsfrom a wrapper or decorator) crashed with an unhandledSystem.IndexOutOfRangeExceptioninMethodBinder.CheckMethodArgumentsMatch. The exception escapes throughtp_callas a CLR exception, so it cannot be caught from Python and tears down the host process (crashes LEAN duringInitialize()).Closes #132
Also bumps the package version to 2.0.60 (package
<Version>,AssemblyVersion/AssemblyFileVersionand the perf-test baseline reference, following the same pattern as #131).Root cause
Since 10e721b (#83), the parameter names array is only populated when there are named arguments (
hasNamedArgs ? methodInformation.ParameterNames : Array.Empty<string>()), but the kwargs code paths inCheckMethodArgumentsMatchandBindonly checked the kwargs dictionary fornull. A non-null but empty kwargs dict therefore routed binding through the kwargs path whileparamNameswas empty, indexing out of range for any overload with more CLR parameters than positional Python arguments. Bisect-confirmed: 10e721b~1 binds fine, 10e721b crashes.Fix
Treat an empty kwargs dict as no keyword arguments:
MethodBinder.Bindonly buildskwArgDictwhenPyDict_Size(kw) > 0, so**{}follows the exact same code path as a plain positional call. This matches pure Python semantics, wheref(x, **{})is equivalent tof(x), and restores the invariant that a non-nullkwArgDictimplies populated parameter names.Testing
TestMethodBinder.BindsOverloadedMethodCalledWithEmptyOrUnpackedKwargscovering empty**{}against overloads of different arity, empty**{}mixed with a named argument, non-empty dict unpacking, overloads with default parameter values, and the*args/**kwargsforwarding idiom from the issue. Without the fix, these crash the test host.quantconnect/lean:foundationcontainer (Python 3.11.14): embedding tests 928/928, pytest suite 454 passed, python_tests_runner 2/2.get_parameterwrapper algorithm crashes on the current binaries and completes successfully with the fixedPython.Runtime.dll.