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
49 changes: 49 additions & 0 deletions Lib/test/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,5 +595,54 @@ class A:
with self.assertRaises(TypeError):
type.__setattr__(A, b'x', None)

def testConstructorErrorMessages(self):
# bpo-31506: Improves the error message logic for object_new & object_init

# Class without any method overrides
class C:
pass

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'):
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'):
object.__init__(C(), 42)

# Class with both `__init__` & `__new__` method overriden
class D:
def __new__(cls, *args, **kwargs):
super().__new__(cls, *args, **kwargs)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

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

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

with self.assertRaisesRegex(TypeError, r'object.__new__\(\) takes no argument'):
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'):
E().__init__(42)

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

if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve the error message logic for object.__new__ and object.__init__.
Patch by Sanyam Khurana.
6 changes: 3 additions & 3 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3592,11 +3592,11 @@ 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() takes no arguments");
PyErr_SetString(PyExc_TypeError, "object.__init__() takes no arguments");
return -1;
}
if (type->tp_new == object_new) {
PyErr_Format(PyExc_TypeError, "%.200s() takes no arguments",
PyErr_Format(PyExc_TypeError, "%.200s().__init__() takes no arguments",
type->tp_name);
return -1;
}
Expand All @@ -3609,7 +3609,7 @@ object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
if (excess_args(args, kwds)) {
if (type->tp_new != object_new) {
PyErr_SetString(PyExc_TypeError, "object() takes no arguments");
PyErr_SetString(PyExc_TypeError, "object.__new__() takes no arguments");
return NULL;
}
if (type->tp_init == object_init) {
Expand Down