Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions Lib/test/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,19 +602,21 @@ def testConstructorErrorMessages(self):
class C:
pass

error_msg = r'C.__init__\(\) takes exactly one argument \(the instance to initialize\)'

with self.assertRaisesRegex(TypeError, r'C\(\) takes no arguments'):
C(42)

with self.assertRaisesRegex(TypeError, r'C\(\) takes no arguments'):
C.__new__(C, 42)

with self.assertRaisesRegex(TypeError, r'C\(\).__init__\(\) takes no arguments'):
with self.assertRaisesRegex(TypeError, error_msg):
C().__init__(42)

with self.assertRaisesRegex(TypeError, r'C\(\) takes no arguments'):
object.__new__(C, 42)

with self.assertRaisesRegex(TypeError, r'C\(\).__init__\(\) takes no arguments'):
with self.assertRaisesRegex(TypeError, error_msg):
object.__init__(C(), 42)

# Class with both `__init__` & `__new__` method overridden
Expand All @@ -624,24 +626,28 @@ def __new__(cls, *args, **kwargs):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

with self.assertRaisesRegex(TypeError, r'object.__new__\(\) takes no argument'):
error_msg = r'object.__new__\(\) takes exactly one argument \(the type to instantiate\)'

with self.assertRaisesRegex(TypeError, error_msg):
D(42)

with self.assertRaisesRegex(TypeError, r'object.__new__\(\) takes no argument'):
with self.assertRaisesRegex(TypeError, error_msg):
D.__new__(D, 42)

with self.assertRaisesRegex(TypeError, r'object.__new__\(\) takes no argument'):
with self.assertRaisesRegex(TypeError, error_msg):
object.__new__(D, 42)

# Class that only overrides __init__
class E:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

with self.assertRaisesRegex(TypeError, r'object.__init__\(\) takes no argument'):
error_msg = r'object.__init__\(\) takes exactly one argument \(the instance to initialize\)'

with self.assertRaisesRegex(TypeError, error_msg):
E().__init__(42)

with self.assertRaisesRegex(TypeError, r'object.__init__\(\) takes no argument'):
with self.assertRaisesRegex(TypeError, error_msg):
object.__init__(E(), 42)

if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Clarify the errors reported when ``object.__new__`` and ``object.__init__``
receive more than one argument.
Contributed by Sanyam Khurana.
9 changes: 6 additions & 3 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3654,11 +3654,13 @@ object_init(PyObject *self, PyObject *args, PyObject *kwds)
PyTypeObject *type = Py_TYPE(self);
if (excess_args(args, kwds)) {
if (type->tp_init != object_init) {
PyErr_SetString(PyExc_TypeError, "object.__init__() takes no arguments");
PyErr_SetString(PyExc_TypeError,
"object.__init__() takes exactly one argument (the instance to initialize)");
return -1;
}
if (type->tp_new == object_new) {
PyErr_Format(PyExc_TypeError, "%.200s().__init__() takes no arguments",
PyErr_Format(PyExc_TypeError,
"%.200s.__init__() takes exactly one argument (the instance to initialize)",
type->tp_name);
return -1;
}
Expand All @@ -3671,7 +3673,8 @@ object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
if (excess_args(args, kwds)) {
if (type->tp_new != object_new) {
PyErr_SetString(PyExc_TypeError, "object.__new__() takes no arguments");
PyErr_SetString(PyExc_TypeError,
"object.__new__() takes exactly one argument (the type to instantiate)");
return NULL;
}
if (type->tp_init == object_init) {
Expand Down