Skip to content

Pinpoint the first mismatched argument in bind-failure TypeErrors - #145

Merged
jhonabreul merged 2 commits into
QuantConnect:masterfrom
jhonabreul:feature-bind-failure-argument-diagnosis
Aug 12, 2026
Merged

Pinpoint the first mismatched argument in bind-failure TypeErrors#145
jhonabreul merged 2 commits into
QuantConnect:masterfrom
jhonabreul:feature-bind-failure-argument-diagnosis

Conversation

@jhonabreul

Copy link
Copy Markdown
Collaborator

What does this implement/fix? Explain your changes.

When a call cannot be matched to any overload, the bind-failure TypeError now pinpoints the first argument that fails to match the nearest overload (the candidate with the most leading convertible arguments), so the caller doesn't have to diff the hinted signatures by eye.

Before:

TypeError: No method matches given arguments for market_order: (<class 'QuantConnect.Symbol'>, <class 'int'>, <class 'str'>). The following overloads are available:
  market_order(symbol: Symbol, quantity: float, asynchronous: bool = False, tag: str = "", order_properties: IOrderProperties = None)
  market_order(symbol: Symbol, quantity: int, asynchronous: bool = False, tag: str = "", order_properties: IOrderProperties = None)

After (last line is new):

TypeError: No method matches given arguments for market_order: (<class 'QuantConnect.Symbol'>, <class 'int'>, <class 'str'>). The following overloads are available:
  market_order(symbol: Symbol, quantity: float, asynchronous: bool = False, tag: str = "", order_properties: IOrderProperties = None)
  market_order(symbol: Symbol, quantity: int, asynchronous: bool = False, tag: str = "", order_properties: IOrderProperties = None)
Argument mismatch: argument 3 ('asynchronous') expected bool, got str.

Keyword arguments whose values cannot convert to the parameter they name are diagnosed too (Argument mismatch: keyword argument 'tag' expected str, got int.).

Details:

  • The diagnosis probe mirrors the binder's per-argument acceptance rules (type alias equality, matching type codes, lossless numeric conversions, implicit operators) and is deliberately lenient where probing is unreliable (by-ref, generic and object parameters, params tails), so it under-reports rather than blames the wrong argument.
  • Pure arity failures append nothing: every given argument matched, so there is no argument to single out.
  • The line is appended after the overloads hint, so Lean's NoMethodMatchPythonExceptionInterpreter, which keeps the message from the The following overloads are available: marker onwards, surfaces it to users unchanged. The No method matches given arguments for {name}: (...) prefix is untouched.
  • It only runs on the error path, never throws, and clears any Python error left pending by conversion probes before the real TypeError is raised.

Does this close any currently open issues?

Part of the pythonnet overload/attribute error-surface improvements (QuantConnect/Agents#305, improvement 1: show the nearest overload with the first mismatching parameter highlighted).

Any other comments?

The fleet's most common failure shape motivating this is a positional string tag landing in the asynchronous: bool slot of the order methods, e.g. self.market_order(symbol, -quantity, "exit signal").

Checklist

Check all those that are applicable and complete.

  • Make sure to include one or more tests for your change
  • If an enhancement PR, please create docs and at best an example
  • Ensure you have signed the .NET Foundation CLA
  • Add yourself to AUTHORS
  • Updated the CHANGELOG

When no overload matches a call, the TypeError now appends a diagnosis of
the first argument that fails to match the nearest overload (the one with
the most leading convertible arguments), e.g.:

  Argument mismatch: argument 3 ('asynchronous') expected bool, got str.

Keyword arguments whose values cannot convert to the matching parameter
are diagnosed too. The line is appended after the overloads hint so
consumers that extract the hint from its marker onwards keep it.
Drops the duplicated type-name helper in favor of the existing runtime
one, and tightens the new comments.
@jhonabreul
jhonabreul marked this pull request as ready for review August 11, 2026 18:06
@jhonabreul
jhonabreul merged commit ad87b8f into QuantConnect:master Aug 12, 2026
9 checks passed
@jhonabreul
jhonabreul deleted the feature-bind-failure-argument-diagnosis branch August 12, 2026 20:32
jhonabreul added a commit to jhonabreul/pythonnet that referenced this pull request Aug 12, 2026
Keeps the Jaro-Winkler scoring from this branch in ClassBase while
retaining Util.LevenshteinDistance, which MethodBinder's unexpected-kwarg
suggestion (QuantConnect#144) still uses.

Also moves the DiagnoseClosestOverloadMismatch block inside the
bind-failure try so master compiles again: QuantConnect#144 wrapped the message
construction (including the candidates declaration) in try/catch after
QuantConnect#145 had added the mismatch diagnosis below it, leaving 'candidates' out
of scope at its use site.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants