[Python-checkins] bpo-31506: Clarify error messages for object.__new__ and object.__init__ (GH-11641)

Miss Islington (bot) webhook-mailer at python.org
Tue Feb 19 08:47:16 EST 2019


https://github.com/python/cpython/commit/64ca72822338e0ba6e4f14d0a1cd3a9dcfa6c9ac
commit: 64ca72822338e0ba6e4f14d0a1cd3a9dcfa6c9ac
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-02-19T05:47:13-08:00
summary:

bpo-31506: Clarify error messages for object.__new__ and object.__init__ (GH-11641)


`object.__new__` and `object.__init__` do take one argument each,
they just don't take extra user supplied arguments.

Patch by Sanyam Khurana.
(cherry picked from commit 5105483acb3aca318304bed056dcfd7e188fe4b5)

Co-authored-by: Sanyam Khurana <8039608+CuriousLearner at users.noreply.github.com>

files:
A Misc/NEWS.d/next/Core and Builtins/2019-01-22-02-06-39.bpo-31506.eJ5FpV.rst
M Lib/test/test_class.py
M Objects/typeobject.c

diff --git a/Lib/test/test_class.py b/Lib/test/test_class.py
index 841cac9171cc..dcaedf43fa90 100644
--- a/Lib/test/test_class.py
+++ b/Lib/test/test_class.py
@@ -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
@@ -624,13 +626,15 @@ 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__
@@ -638,10 +642,12 @@ 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__':
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
index 000000000000..9ebcab7e2a71
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2019-01-22-02-06-39.bpo-31506.eJ5FpV.rst	
@@ -0,0 +1,3 @@
+Clarify the errors reported when ``object.__new__`` and ``object.__init__``
+receive more than one argument.
+Contributed by Sanyam Khurana.
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 468210574119..c578d6e93545 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -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;
         }
@@ -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) {



More information about the Python-checkins mailing list