summaryrefslogtreecommitdiffstats
path: root/Python/ceval.c
diff options
context:
space:
mode:
authorPablo Galindo Salgado <Pablogsal@gmail.com>2023-08-17 18:39:42 (GMT)
committerGitHub <noreply@github.com>2023-08-17 18:39:42 (GMT)
commit75b3db8445188c2ad38cabc0021af694df0829b8 (patch)
tree0787c79b66363158b539a0de86a0b3ef6c37d0eb /Python/ceval.c
parent61c7249759ce88465ea655d5c19d17d03ff3f74b (diff)
downloadcpython-75b3db8445188c2ad38cabc0021af694df0829b8.zip
cpython-75b3db8445188c2ad38cabc0021af694df0829b8.tar.gz
cpython-75b3db8445188c2ad38cabc0021af694df0829b8.tar.bz2
gh-107944: Improve error message for function calls with bad keyword arguments (#107969)
Diffstat (limited to 'Python/ceval.c')
-rw-r--r--Python/ceval.c31
1 files changed, 28 insertions, 3 deletions
diff --git a/Python/ceval.c b/Python/ceval.c
index 329a1a1..f7dfaeb 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -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;
}