]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-31506: Clarify error messages for object.__new__ and object.__init__ (GH-11641)
authorSanyam Khurana <8039608+CuriousLearner@users.noreply.github.com>
Tue, 19 Feb 2019 13:23:48 +0000 (18:53 +0530)
committerNick Coghlan <ncoghlan@gmail.com>
Tue, 19 Feb 2019 13:23:48 +0000 (23:23 +1000)
`object.__new__` and `object.__init__` do take one argument each,
they just don't take extra user supplied arguments.

Patch by Sanyam Khurana.

Lib/test/test_class.py
Misc/NEWS.d/next/Core and Builtins/2019-01-22-02-06-39.bpo-31506.eJ5FpV.rst [new file with mode: 0644]
Objects/typeobject.c

index 998452a10eee8eea12236381a50a9ccf9b48afca..456f1be30be04628409163fe7057744294bec7c4 100644 (file)
@@ -618,19 +618,21 @@ class ClassTests(unittest.TestCase):
         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
@@ -640,13 +642,15 @@ class ClassTests(unittest.TestCase):
             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__
@@ -654,10 +658,12 @@ class ClassTests(unittest.TestCase):
             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__':
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-01-22-02-06-39.bpo-31506.eJ5FpV.rst b/Misc/NEWS.d/next/Core and Builtins/2019-01-22-02-06-39.bpo-31506.eJ5FpV.rst
new file mode 100644 (file)
index 0000000..9ebcab7
--- /dev/null
@@ -0,0 +1,3 @@
+Clarify the errors reported when ``object.__new__`` and ``object.__init__``
+receive more than one argument.
+Contributed by Sanyam Khurana.
index ace45ba5794ba44eb5798de1508b10c75d1b58b4..e6cf4fbf30aeb6ae5643c3c5cf2fed5996b80977 100644 (file)
@@ -3681,11 +3681,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;
         }
@@ -3698,7 +3700,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) {