-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
a513ee8
4d792e4
d976676
d811b4c
70d57c2
b6f75e4
390f941
d2e54e6
0ac8a1c
59bca2d
51a6c79
699b112
06606e4
8c142f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,8 +129,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 type int (not %T)", | ||
o, result); | ||
Py_DECREF(result); | ||
return -1; | ||
} | ||
|
@@ -140,7 +141,9 @@ 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 positive int (not %T)", | ||
o, result); | ||
return -1; | ||
} | ||
return res; | ||
|
@@ -884,8 +887,8 @@ PyObject_Format(PyObject *obj, PyObject *format_spec) | |
|
||
if (result && !PyUnicode_Check(result)) { | ||
PyErr_Format(PyExc_TypeError, | ||
"__format__ must return a str, not %.200s", | ||
donBarbos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Py_TYPE(result)->tp_name); | ||
"%T.__format__ must return type str (not %T)", | ||
obj, result); | ||
Py_SETREF(result, NULL); | ||
goto done; | ||
} | ||
|
@@ -1418,13 +1421,14 @@ _PyNumber_Index(PyObject *item) | |
|
||
if (!PyLong_Check(result)) { | ||
PyErr_Format(PyExc_TypeError, | ||
"%T.__index__ returned non-int (type %T)", item, result); | ||
"%T.__index__ must return type 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, | ||
"%T.__index__ returned non-int (type %T). " | ||
"%T.__index__ must return type 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.", | ||
item, result)) { | ||
|
@@ -1527,13 +1531,13 @@ PyNumber_Long(PyObject *o) | |
|
||
if (!PyLong_Check(result)) { | ||
PyErr_Format(PyExc_TypeError, | ||
"%T.__int__ returned non-int (type %T)", o, result); | ||
"%T.__int__ must return type 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, | ||
"%T.__int__ returned non-int (type %T). " | ||
"%T.__int__ must return type 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.", | ||
o, result)) { | ||
|
@@ -1604,13 +1608,14 @@ PyNumber_Float(PyObject *o) | |
|
||
if (!PyFloat_Check(res)) { | ||
PyErr_Format(PyExc_TypeError, | ||
"%T.__float__ returned non-float (type %T)", o, res); | ||
"%T.__float__ must return type 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__ returned non-float (type %T). " | ||
"%T.__float__ must return type 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.", | ||
o, res)) { | ||
|
@@ -2429,10 +2434,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)", | ||
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: |
||
Py_TYPE(o)->tp_name, | ||
meth, | ||
Py_TYPE(meth_output)->tp_name); | ||
"%T.%U() must return type iterable (not %T)", | ||
o, meth, meth_output); | ||
} | ||
Py_DECREF(meth_output); | ||
return NULL; | ||
|
@@ -2812,7 +2815,7 @@ PyObject_GetIter(PyObject *o) | |
PyObject *res = (*f)(o); | ||
if (res != NULL && !PyIter_Check(res)) { | ||
PyErr_Format(PyExc_TypeError, | ||
"%T.iter() returned non-iterator of type '%T'", | ||
"%T.iter() must return type iterator of type '%T'", | ||
o, res); | ||
Py_SETREF(res, NULL); | ||
} | ||
|
@@ -2832,8 +2835,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 resolved
Hide resolved
|
||
Py_TYPE(it)->tp_name); | ||
"%T.aiter() must return type async iterator of type '%T'", | ||
o, it); | ||
Py_SETREF(it, NULL); | ||
} | ||
return it; | ||
|
Uh oh!
There was an error while loading. Please reload this page.