Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-130821: Add type information to wrong type error messages#130835
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
a513ee84d792e4d976676d811b4c70d57c2b6f75e4390f941d2e54e60ac8a1c59bca2d51a6c79699b11206606e48c142f397cf25c22baff629afd3cFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Enhance wrong type error messages and make them more consistent. Patch by | ||
| Semyon Moroz. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -132,8 +132,9 @@ PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue) | ||
| return defaultvalue; | ||
| } | ||
| if (!PyLong_Check(result)){ | ||
| PyErr_Format(PyExc_TypeError, "__length_hint__ must be an integer, not %.100s", | ||
| Py_TYPE(result)->tp_name); | ||
| PyErr_Format(PyExc_TypeError, | ||
| "%T.__length_hint__() must return an int, not %T", | ||
| o, result); | ||
| Py_DECREF(result); | ||
| return -1; | ||
| } | ||
| @@ -143,7 +144,8 @@ PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue) | ||
| return -1; | ||
| } | ||
| if (res < 0){ | ||
| PyErr_Format(PyExc_ValueError, "__length_hint__() should return >= 0"); | ||
| PyErr_Format(PyExc_ValueError, | ||
| "%T.__length_hint__() must return a non-negative int", o); | ||
| return -1; | ||
| } | ||
| return res; | ||
| @@ -887,8 +889,8 @@ PyObject_Format(PyObject *obj, PyObject *format_spec) | ||
| if (result && !PyUnicode_Check(result)){ | ||
| PyErr_Format(PyExc_TypeError, | ||
| "__format__ must return a str, not %.200s", | ||
| Py_TYPE(result)->tp_name); | ||
| "%T.__format__() must return a str, not %T", | ||
| obj, result); | ||
| Py_SETREF(result, NULL); | ||
| goto done; | ||
| } | ||
| @@ -1421,17 +1423,17 @@ _PyNumber_Index(PyObject *item) | ||
| if (!PyLong_Check(result)){ | ||
| PyErr_Format(PyExc_TypeError, | ||
| "__index__ returned non-int (type %.200s)", | ||
Member 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 don't see much reason to change this error message either. ContributorAuthor
| ||
| Py_TYPE(result)->tp_name); | ||
| "%T.__index__() must return an int, not %T", | ||
| item, result); | ||
| Py_DECREF(result); | ||
| return NULL; | ||
| } | ||
| /* Issue #17576: warn if 'result' not of exact type int. */ | ||
| if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, | ||
| "__index__ returned non-int (type %.200s). " | ||
| "%T.__index__() must return an int, not %T. " | ||
| "The ability to return an instance of a strict subclass of int " | ||
| "is deprecated, and may be removed in a future version of Python.", | ||
| Py_TYPE(result)->tp_name)){ | ||
| item, result)){ | ||
| Py_DECREF(result); | ||
| return NULL; | ||
| } | ||
| @@ -1531,17 +1533,17 @@ PyNumber_Long(PyObject *o) | ||
| if (!PyLong_Check(result)){ | ||
| PyErr_Format(PyExc_TypeError, | ||
| "__int__ returned non-int (type %.200s)", | ||
| Py_TYPE(result)->tp_name); | ||
| "%T.__int__() must return an int, not %T", | ||
| o, result); | ||
| Py_DECREF(result); | ||
| return NULL; | ||
| } | ||
| /* Issue #17576: warn if 'result' not of exact type int. */ | ||
| if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, | ||
| "__int__ returned non-int (type %.200s). " | ||
| "%T.__int__() must return an int, not %T. " | ||
| "The ability to return an instance of a strict subclass of int " | ||
| "is deprecated, and may be removed in a future version of Python.", | ||
| Py_TYPE(result)->tp_name)){ | ||
| o, result)){ | ||
| Py_DECREF(result); | ||
| return NULL; | ||
| } | ||
| @@ -1609,17 +1611,16 @@ PyNumber_Float(PyObject *o) | ||
| if (!PyFloat_Check(res)){ | ||
| PyErr_Format(PyExc_TypeError, | ||
| "%.50s.__float__ returned non-float (type %.50s)", | ||
| Py_TYPE(o)->tp_name, Py_TYPE(res)->tp_name); | ||
| "%T.__float__() must return a float, not %T", o, res); | ||
| Py_DECREF(res); | ||
| return NULL; | ||
| } | ||
| /* Issue #26983: warn if 'res' not of exact type float. */ | ||
| if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, | ||
| "%.50s.__float__ returned non-float (type %.50s). " | ||
| "%T.__float__() must return a float, not %T. " | ||
| "The ability to return an instance of a strict subclass of float " | ||
| "is deprecated, and may be removed in a future version of Python.", | ||
| Py_TYPE(o)->tp_name, Py_TYPE(res)->tp_name)){ | ||
| o, res)){ | ||
| Py_DECREF(res); | ||
| return NULL; | ||
| } | ||
| @@ -2435,10 +2436,8 @@ method_output_as_list(PyObject *o, PyObject *meth) | ||
| PyThreadState *tstate = _PyThreadState_GET(); | ||
| if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError)){ | ||
| _PyErr_Format(tstate, PyExc_TypeError, | ||
| "%.200s.%U() returned a non-iterable (type %.200s)", | ||
Member 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. Here I strongly prefer the old message. Iterable is not a type, it's a category of types. ContributorAuthor 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. what do you think about new message: Member 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 don't think new message adds something bad. Why someone might think, that iterable is a type here? (Another similar case, where new messages mention an iterator.) ContributorAuthor 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.
it's just that in the previous iteration the error message was the phrase "must return type ..." :) | ||
| Py_TYPE(o)->tp_name, | ||
| meth, | ||
| Py_TYPE(meth_output)->tp_name); | ||
| "%T.%U() must return an iterable, not %T", | ||
| o, meth, meth_output); | ||
| } | ||
| Py_DECREF(meth_output); | ||
| return NULL; | ||
| @@ -2818,9 +2817,8 @@ PyObject_GetIter(PyObject *o) | ||
| PyObject *res = (*f)(o); | ||
| if (res != NULL && !PyIter_Check(res)){ | ||
| PyErr_Format(PyExc_TypeError, | ||
| "iter() returned non-iterator " | ||
| "of type '%.100s'", | ||
| Py_TYPE(res)->tp_name); | ||
| "%T.__iter__() must return an iterator, not %T", | ||
| o, res); | ||
| Py_SETREF(res, NULL); | ||
| } | ||
| return res; | ||
| @@ -2839,8 +2837,8 @@ PyObject_GetAIter(PyObject *o){ | ||
| PyObject *it = (*f)(o); | ||
| if (it != NULL && !PyAIter_Check(it)){ | ||
| PyErr_Format(PyExc_TypeError, | ||
| "aiter() returned not an async iterator of type '%.100s'", | ||
donbarbos marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| Py_TYPE(it)->tp_name); | ||
| "%T.__aiter__() must return an async iterator, not %T", | ||
| o, it); | ||
| Py_SETREF(it, NULL); | ||
| } | ||
| return it; | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.