Skip to content

Conversation

@KevinRK29
Copy link
Collaborator

@KevinRK29 KevinRK29 commented Jan 16, 2026

This PR improves error messages when calling overloaded functions with unexpected keyword arguments, making it easier to identify and fix typos.

Changes

  • Add suggestions for misspelled keyword argument names (using existing best_matches fuzzy matching)
  • Include the line number where a type-compatible overload variant is defined
  • Fall back to showing all possible overload variants when no type-matching suggestion can be made

@github-actions

This comment has been minimized.

def f(foobar: Union[int, str]) -> None:
pass

f(fobar=1) # E: Unexpected keyword argument "fobar" for overloaded function "f" defined on line 4; did you mean "foobar"?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't report the line number here. It could be useful to report it, but we generally use a note, since the function could be in a different file so line number by itself isn't sufficient.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point, I'll exclude the line number from the error messaging

pass

f(fobar=1) # E: Unexpected keyword argument "fobar" for overloaded function "f" defined on line 4; did you mean "foobar"?
f(random=[1,2,3]) # E: Unexpected keyword argument "random" for overloaded function "f" \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional test ideas:

  • Test multiple invalid keyword arguments
  • Test both invalid keyword argument and incompatible positional argument
  • Test both valid an invalid keyword arguments in the same call

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

prefect (https://github.com/PrefectHQ/prefect)
+ src/prefect/futures.py:223: error: Unexpected keyword argument "_sync" for overloaded function "result" of "State"  [call-overload]
+ src/prefect/utilities/engine.py:764: error: Unexpected keyword argument "_sync" for overloaded function "result" of "State"  [call-overload]
+ src/prefect/task_engine.py:529: error: Unexpected keyword argument "_sync" for overloaded function "result" of "State"  [call-overload]

scipy (https://github.com/scipy/scipy)
+ scipy/sparse/linalg/tests/test_interface.py:306: error: Unexpected keyword argument "axis" for overloaded function "__call__" of "_GUFunc_Nin2_Nout1"  [call-overload]
+ scipy/sparse/linalg/tests/test_interface.py:307: error: Unexpected keyword argument "axis" for overloaded function "__call__" of "_GUFunc_Nin2_Nout1"  [call-overload]

xarray (https://github.com/pydata/xarray)
+ xarray/tests/test_plot.py:1167: error: Unexpected keyword argument "start" for overloaded function "arange"  [call-overload]
+ xarray/tests/test_plot.py:1168: error: Unexpected keyword argument "start" for overloaded function "arange"  [call-overload]

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