Skip to content

Improve error messages for unexpected keyword arguments in overloaded functions#20592

Open
KevinRK29 wants to merge 11 commits intopython:masterfrom
KevinRK29:improve-overloaded-error-messages
Open

Improve error messages for unexpected keyword arguments in overloaded functions#20592
KevinRK29 wants to merge 11 commits intopython:masterfrom
KevinRK29:improve-overloaded-error-messages

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.

@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

This comment has been minimized.

@KevinRK29 KevinRK29 force-pushed the improve-overloaded-error-messages branch from 08e94c3 to f3067fd Compare January 30, 2026 05:42
@github-actions

This comment has been minimized.

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 30, 2026

Can you investigate the mypy_primer diff? Were there no errors reported in the past, or do we now generate multiple errors for some calls, and are the new errors valid?

@KevinRK29
Copy link
Collaborator Author

KevinRK29 commented Feb 19, 2026

@JukkaL so looking into this:

prefect (https://github.com/PrefectHQ/prefect)
+ src/prefect/futures.py:224: 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]

this seems like it's because none of the function result overloads have the parameter _sync. Apparently the parameter is injected with a decorator @async_dispatch. So it works at runtime but fails during type checking. So before this PR, it would have already had a "No overload variant of 'result' matches argument types..." error but now we throw an additional unexpected keyword argument error as well. However they type ignore the line of code (1, 2) with reportCallIssue (except this).

@KevinRK29
Copy link
Collaborator Author

KevinRK29 commented Feb 20, 2026

This is the same trend as above with the one in the scipy repo where the __call__ overloaded function takes in axes (plural), but it allows the usage of the parameter axis during runtime.

And for the third one, the np.arange where it passes in a start keyword argument when the python function doesn't define it. But this function is written in C, so it has it's own argument parsing which allows the start keyword.

So there were errors reported in the past for all of these instances but they were of the generic message like No overload variant of "result" of "State" matches argument types .... But now we add this specific error messaging alongside it, which seems valid to me.

@JukkaL
Copy link
Collaborator

JukkaL commented Feb 20, 2026

But now we add this specific error messaging alongside it, which seems valid to me.

Yeah, that is a reasonable change.

@KevinRK29 KevinRK29 marked this pull request as ready for review February 20, 2026 15:56
@KevinRK29 KevinRK29 requested a review from JukkaL February 20, 2026 15:57

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

f(fobar=1) # E: Unexpected keyword argument "fobar" for overloaded function "f"; 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.

Additional test case ideas:

  • Test a call where there are multiple positional and keyword arguments, and everything else is valid, but one of the keyword arguments is a misspelling (and not the first one).
  • Test a call where there are multiple keyword arguments, and two of them are misspelling of different target keyword argument names, but otherwise the call is fine.
  • A test case like obj.f(fobar=1), where we have a misspelling in a method call.

@KevinRK29 KevinRK29 requested a review from JukkaL March 16, 2026 06:11
@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:224: error: Unexpected keyword argument "_sync" for overloaded function "result" of "State"  [call-overload]
+ src/prefect/utilities/engine.py:765: error: Unexpected keyword argument "_sync" for overloaded function "result" of "State"  [call-overload]
+ src/prefect/task_engine.py:530: error: Unexpected keyword argument "_sync" for overloaded function "result" of "State"  [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