diff options
author | Sanyam Khurana <8039608+CuriousLearner@users.noreply.github.com> | 2019-02-19 13:23:48 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2019-02-19 13:23:48 (GMT) |
commit | 5105483acb3aca318304bed056dcfd7e188fe4b5 (patch) | |
tree | d0597928b1f356700d4b0d841a49e1827b07a915 /Lib/test/test_class.py | |
parent | f522a57ec77921ee2e60bd4ccda3c8daa5a43e95 (diff) | |
download | cpython-5105483acb3aca318304bed056dcfd7e188fe4b5.zip cpython-5105483acb3aca318304bed056dcfd7e188fe4b5.tar.gz cpython-5105483acb3aca318304bed056dcfd7e188fe4b5.tar.bz2 |
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.
Diffstat (limited to 'Lib/test/test_class.py')
-rw-r--r-- | Lib/test/test_class.py | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/Lib/test/test_class.py b/Lib/test/test_class.py index 998452a..456f1be 100644 --- a/Lib/test/test_class.py +++ b/Lib/test/test_class.py @@ -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__': |