Skip to content

Commit 390f941

Browse files
committed
Improve message and fix test
1 parent b6f75e4 commit 390f941

6 files changed

Lines changed: 18 additions & 17 deletions

File tree

Lib/test/test_type_annotations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def check_annotations(self, f):
301301
print(f.__annotations__)
302302

303303
f.__annotate__ = lambda x: 42
304-
with self.assertRaisesRegex(TypeError, r"__annotate__ returned non-dict of type 'int'"):
304+
with self.assertRaisesRegex(TypeError, r"__annotate__() must return type dict of type 'int'"):
305305
print(f.__annotations__)
306306

307307
f.__annotate__ = lambda x: {"x": x}

Objects/abstract.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,8 @@ PyObject_Format(PyObject *obj, PyObject *format_spec)
887887

888888
if (result && !PyUnicode_Check(result)) {
889889
PyErr_Format(PyExc_TypeError,
890-
"__format__ must return type str (not %T)", result);
890+
"%T.__format__() must return type str (not %T)",
891+
obj, result);
891892
Py_SETREF(result, NULL);
892893
goto done;
893894
}
@@ -1420,14 +1421,14 @@ _PyNumber_Index(PyObject *item)
14201421

14211422
if (!PyLong_Check(result)) {
14221423
PyErr_Format(PyExc_TypeError,
1423-
"%T.__index__ must return type int (not %T)",
1424+
"%T.__index__() must return type int (not %T)",
14241425
item, result);
14251426
Py_DECREF(result);
14261427
return NULL;
14271428
}
14281429
/* Issue #17576: warn if 'result' not of exact type int. */
14291430
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1430-
"%T.__index__ must return type int (not %T). "
1431+
"%T.__index__() must return type int (not %T). "
14311432
"The ability to return an instance of a strict subclass of int "
14321433
"is deprecated, and may be removed in a future version of Python.",
14331434
item, result)) {
@@ -1530,14 +1531,14 @@ PyNumber_Long(PyObject *o)
15301531

15311532
if (!PyLong_Check(result)) {
15321533
PyErr_Format(PyExc_TypeError,
1533-
"%T.__int__ must return type int (not %T)",
1534+
"%T.__int__() must return type int (not %T)",
15341535
o, result);
15351536
Py_DECREF(result);
15361537
return NULL;
15371538
}
15381539
/* Issue #17576: warn if 'result' not of exact type int. */
15391540
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1540-
"%T.__int__ must return type int (not %T). "
1541+
"%T.__int__() must return type int (not %T). "
15411542
"The ability to return an instance of a strict subclass of int "
15421543
"is deprecated, and may be removed in a future version of Python.",
15431544
o, result)) {
@@ -1608,13 +1609,13 @@ PyNumber_Float(PyObject *o)
16081609

16091610
if (!PyFloat_Check(res)) {
16101611
PyErr_Format(PyExc_TypeError,
1611-
"%T.__float__ must return type float (not %T)", o, res);
1612+
"%T.__float__() must return type float (not %T)", o, res);
16121613
Py_DECREF(res);
16131614
return NULL;
16141615
}
16151616
/* Issue #26983: warn if 'res' not of exact type float. */
16161617
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1617-
"%T.__float__ must return type float (not %T). "
1618+
"%T.__float__() must return type float (not %T). "
16181619
"The ability to return an instance of a strict subclass of float "
16191620
"is deprecated, and may be removed in a future version of Python.",
16201621
o, res)) {

Objects/bytesobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ format_obj(PyObject *v, const char **pbuf, Py_ssize_t *plen)
566566
return NULL;
567567
if (!PyBytes_Check(result)) {
568568
PyErr_Format(PyExc_TypeError,
569-
"%T.__bytes__ must return type bytes (not %T)",
569+
"%T.__bytes__() must return type bytes (not %T)",
570570
v, result);
571571
Py_DECREF(result);
572572
return NULL;
@@ -2788,7 +2788,7 @@ bytes_new_impl(PyTypeObject *type, PyObject *x, const char *encoding,
27882788
return NULL;
27892789
if (!PyBytes_Check(bytes)) {
27902790
PyErr_Format(PyExc_TypeError,
2791-
"%T.__bytes__ must return type bytes (not %T)",
2791+
"%T.__bytes__() must return type bytes (not %T)",
27922792
x, bytes);
27932793
Py_DECREF(bytes);
27942794
return NULL;

Objects/complexobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,14 +499,14 @@ try_complex_special_method(PyObject *op)
499499
}
500500
if (!PyComplex_Check(res)) {
501501
PyErr_Format(PyExc_TypeError,
502-
"%T.__complex__ must return type complex (not %T)",
502+
"%T.__complex__() must return type complex (not %T)",
503503
op, res);
504504
Py_DECREF(res);
505505
return NULL;
506506
}
507507
/* Issue #29894: warn if 'res' not of exact type complex. */
508508
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
509-
"%T.__complex__ must return type complex (not %T). "
509+
"%T.__complex__() must return type complex (not %T). "
510510
"The ability to return an instance of a strict subclass of complex "
511511
"is deprecated, and may be removed in a future version of Python.",
512512
op, res)) {

Objects/object.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ PyObject_Repr(PyObject *v)
781781
}
782782
if (!PyUnicode_Check(res)) {
783783
_PyErr_Format(tstate, PyExc_TypeError,
784-
"%T.__repr__ must return type str (not %T)", v, res);
784+
"%T.__repr__() must return type str (not %T)", v, res);
785785
Py_DECREF(res);
786786
return NULL;
787787
}
@@ -823,7 +823,7 @@ PyObject_Str(PyObject *v)
823823
}
824824
if (!PyUnicode_Check(res)) {
825825
_PyErr_Format(tstate, PyExc_TypeError,
826-
"%T.__str__ must return type str (not %T)", v, res);
826+
"%T.__str__() must return type str (not %T)", v, res);
827827
Py_DECREF(res);
828828
return NULL;
829829
}
@@ -878,7 +878,7 @@ PyObject_Bytes(PyObject *v)
878878
return NULL;
879879
if (!PyBytes_Check(result)) {
880880
PyErr_Format(PyExc_TypeError,
881-
"%T.__bytes__ must return type bytes (not %T)",
881+
"%T.__bytes__() must return type bytes (not %T)",
882882
v, result);
883883
Py_DECREF(result);
884884
return NULL;

Objects/typeobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,7 +2029,7 @@ type_get_annotations(PyObject *tp, void *Py_UNUSED(closure))
20292029
}
20302030
if (!PyDict_Check(annotations)) {
20312031
PyErr_Format(PyExc_TypeError,
2032-
"__annotate__ must return type dict of type '%T'",
2032+
"__annotate__() must return type dict of type '%T'",
20332033
annotations);
20342034
Py_DECREF(annotations);
20352035
Py_DECREF(annotate);
@@ -10077,7 +10077,7 @@ slot_nb_bool(PyObject *self)
1007710077
}
1007810078
else {
1007910079
PyErr_Format(PyExc_TypeError,
10080-
"%T.__bool__ must return type bool (not %T)",
10080+
"%T.__bool__() must return type bool (not %T)",
1008110081
self, value);
1008210082
result = -1;
1008310083
}

0 commit comments

Comments
 (0)