Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Changes fromall commits
a513ee8
4d792e4
d976676
d811b4c
70d57c2
b6f75e4
390f941
d2e54e6
0ac8a1c
59bca2d
51a6c79
699b112
06606e4
File 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, | ||
"%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, | ||
"%T.__length_hint__() must return a positive 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, | ||
donBarbos marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
"%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, | ||
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
| ||
"%T.__index__() must return anint, 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, | ||
"%T.__index__() must return anint, 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.", | ||
item,result)) { | ||
Py_DECREF(result); | ||
return NULL; | ||
} | ||
@@ -1531,17 +1533,17 @@ PyNumber_Long(PyObject *o) | ||
if (!PyLong_Check(result)) { | ||
PyErr_Format(PyExc_TypeError, | ||
"%T.__int__() must return anint, 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, | ||
"%T.__int__() must return anint, 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.", | ||
o,result)) { | ||
Py_DECREF(result); | ||
return NULL; | ||
} | ||
@@ -1609,17 +1611,16 @@ PyNumber_Float(PyObject *o) | ||
if (!PyFloat_Check(res)) { | ||
PyErr_Format(PyExc_TypeError, | ||
"%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, | ||
"%T.__float__() must return afloat, 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.", | ||
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, | ||
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. 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: | ||
"%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, | ||
"%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, | ||
donBarbos marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
"%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.