Improve error messages for unexpected keyword arguments in overloaded functions#20592
Improve error messages for unexpected keyword arguments in overloaded functions#20592KevinRK29 wants to merge 11 commits intopython:masterfrom
Conversation
This comment has been minimized.
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"? |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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" \ |
There was a problem hiding this comment.
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
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
08e94c3 to
f3067fd
Compare
This comment has been minimized.
This comment has been minimized.
|
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? |
|
@JukkaL so looking into this: this seems like it's because none of the function |
|
This is the same trend as above with the one in the scipy repo where the 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 |
Yeah, that is a reasonable change. |
|
|
||
| def f(foobar: Union[int, str]) -> None: pass | ||
|
|
||
| f(fobar=1) # E: Unexpected keyword argument "fobar" for overloaded function "f"; did you mean "foobar"? |
There was a problem hiding this comment.
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.
|
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]
|
This PR improves error messages when calling overloaded functions with unexpected keyword arguments, making it easier to identify and fix typos.