mirror of
https://github.com/python/cpython.git
synced 2025-08-22 17:55:18 +00:00
gh-107944: Improve error message for function calls with bad keyword arguments (#107969)
This commit is contained in:
parent
61c7249759
commit
75b3db8445
5 changed files with 106 additions and 11 deletions
|
@ -26,6 +26,7 @@
|
|||
#include "pycore_tuple.h" // _PyTuple_ITEMS()
|
||||
#include "pycore_typeobject.h" // _PySuper_Lookup()
|
||||
#include "pycore_uops.h" // _PyUOpExecutorObject
|
||||
#include "pycore_pyerrors.h"
|
||||
|
||||
#include "pycore_dict.h"
|
||||
#include "dictobject.h"
|
||||
|
@ -1337,9 +1338,33 @@ initialize_locals(PyThreadState *tstate, PyFunctionObject *func,
|
|||
goto kw_fail;
|
||||
}
|
||||
|
||||
_PyErr_Format(tstate, PyExc_TypeError,
|
||||
"%U() got an unexpected keyword argument '%S'",
|
||||
func->func_qualname, keyword);
|
||||
PyObject* suggestion_keyword = NULL;
|
||||
if (total_args > co->co_posonlyargcount) {
|
||||
PyObject* possible_keywords = PyList_New(total_args - co->co_posonlyargcount);
|
||||
|
||||
if (!possible_keywords) {
|
||||
PyErr_Clear();
|
||||
} else {
|
||||
for (Py_ssize_t k = co->co_posonlyargcount; k < total_args; k++) {
|
||||
PyList_SET_ITEM(possible_keywords, k - co->co_posonlyargcount, co_varnames[k]);
|
||||
}
|
||||
|
||||
suggestion_keyword = _Py_CalculateSuggestions(possible_keywords, keyword);
|
||||
Py_DECREF(possible_keywords);
|
||||
}
|
||||
}
|
||||
|
||||
if (suggestion_keyword) {
|
||||
_PyErr_Format(tstate, PyExc_TypeError,
|
||||
"%U() got an unexpected keyword argument '%S'. Did you mean '%S'?",
|
||||
func->func_qualname, keyword, suggestion_keyword);
|
||||
Py_DECREF(suggestion_keyword);
|
||||
} else {
|
||||
_PyErr_Format(tstate, PyExc_TypeError,
|
||||
"%U() got an unexpected keyword argument '%S'",
|
||||
func->func_qualname, keyword);
|
||||
}
|
||||
|
||||
goto kw_fail;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue