-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-107944: Improve error message for getargs with bad keyword arguments #114792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f78eaeb
gh-107944: Improve error message for getargs with bad keyword arguments
hauntsaninja 713f85a
📜🤖 Added by blurb_it.
blurb-it[bot] a303953
add a what's new
hauntsaninja 7a3804d
add another test case
hauntsaninja d178541
Fix vgetargskeywords path, explicit error test
hauntsaninja ec06393
nit: change test order
hauntsaninja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Core and Builtins/2024-01-31-09-10-10.gh-issue-107944.XWm1B-.rst
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Improve error message for function calls with bad keyword arguments via getargs |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
#include "pycore_modsupport.h" // export _PyArg_NoKeywords() | ||
#include "pycore_pylifecycle.h" // _PyArg_Fini | ||
#include "pycore_tuple.h" // _PyTuple_ITEMS() | ||
#include "pycore_pyerrors.h" // _Py_CalculateSuggestions() | ||
|
||
/* Export Stable ABIs (abi only) */ | ||
PyAPI_FUNC(int) _PyArg_Parse_SizeT(PyObject *, const char *, ...); | ||
|
@@ -1424,12 +1425,31 @@ error_unexpected_keyword_arg(PyObject *kwargs, PyObject *kwnames, PyObject *kwtu | |
int match = PySequence_Contains(kwtuple, keyword); | ||
if (match <= 0) { | ||
if (!match) { | ||
PyErr_Format(PyExc_TypeError, | ||
"'%S' is an invalid keyword " | ||
"argument for %.200s%s", | ||
keyword, | ||
(fname == NULL) ? "this function" : fname, | ||
(fname == NULL) ? "" : "()"); | ||
PyObject *kwlist = PySequence_List(kwtuple); | ||
if (!kwlist) { | ||
return; | ||
} | ||
PyObject *suggestion_keyword = _Py_CalculateSuggestions(kwlist, keyword); | ||
Py_DECREF(kwlist); | ||
|
||
if (suggestion_keyword) { | ||
PyErr_Format(PyExc_TypeError, | ||
"%.200s%s got an unexpected keyword argument '%S'." | ||
" Did you mean '%S'?", | ||
(fname == NULL) ? "this function" : fname, | ||
(fname == NULL) ? "" : "()", | ||
keyword, | ||
suggestion_keyword); | ||
Py_DECREF(suggestion_keyword); | ||
} | ||
else { | ||
PyErr_Format(PyExc_TypeError, | ||
"%.200s%s got an unexpected keyword argument '%S'", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made this error message match the one in ceval.c |
||
(fname == NULL) ? "this function" : fname, | ||
(fname == NULL) ? "" : "()", | ||
keyword); | ||
} | ||
|
||
} | ||
return; | ||
} | ||
|
@@ -1457,6 +1477,9 @@ PyArg_ValidateKeywordArguments(PyObject *kwargs) | |
return 1; | ||
} | ||
|
||
static PyObject * | ||
new_kwtuple(const char * const *keywords, int total, int pos); | ||
|
||
#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':') | ||
|
||
static int | ||
|
@@ -1722,12 +1745,35 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format, | |
} | ||
} | ||
if (!match) { | ||
PyErr_Format(PyExc_TypeError, | ||
"'%U' is an invalid keyword " | ||
"argument for %.200s%s", | ||
key, | ||
(fname == NULL) ? "this function" : fname, | ||
(fname == NULL) ? "" : "()"); | ||
PyObject *_pykwtuple = new_kwtuple(kwlist, len, pos); | ||
if (!_pykwtuple) { | ||
return cleanreturn(0, &freelist); | ||
} | ||
PyObject *pykwlist = PySequence_List(_pykwtuple); | ||
Py_DECREF(_pykwtuple); | ||
if (!pykwlist) { | ||
return cleanreturn(0, &freelist); | ||
} | ||
PyObject *suggestion_keyword = _Py_CalculateSuggestions(pykwlist, key); | ||
Py_DECREF(pykwlist); | ||
|
||
if (suggestion_keyword) { | ||
PyErr_Format(PyExc_TypeError, | ||
"%.200s%s got an unexpected keyword argument '%S'." | ||
" Did you mean '%S'?", | ||
(fname == NULL) ? "this function" : fname, | ||
(fname == NULL) ? "" : "()", | ||
key, | ||
suggestion_keyword); | ||
Py_DECREF(suggestion_keyword); | ||
} | ||
else { | ||
PyErr_Format(PyExc_TypeError, | ||
"%.200s%s got an unexpected keyword argument '%S'", | ||
(fname == NULL) ? "this function" : fname, | ||
(fname == NULL) ? "" : "()", | ||
key); | ||
} | ||
return cleanreturn(0, &freelist); | ||
} | ||
} | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mind adding a explicit test for when the suggestion fails (the 'else' case in the conditional here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review! I added this test.
I also made a similar edit in the
vgetargskeywords
code path and added tests.