| 740 | del B().z |
| 741 | |
| 742 | def testConstructorErrorMessages(self): |
| 743 | # bpo-31506: Improves the error message logic for object_new & object_init |
| 744 | |
| 745 | # Class without any method overrides |
| 746 | class C: |
| 747 | pass |
| 748 | |
| 749 | error_msg = r'C.__init__\(\) takes exactly one argument \(the instance to initialize\)' |
| 750 | |
| 751 | with self.assertRaisesRegex(TypeError, r'C\(\) takes no arguments'): |
| 752 | C(42) |
| 753 | |
| 754 | with self.assertRaisesRegex(TypeError, r'C\(\) takes no arguments'): |
| 755 | C.__new__(C, 42) |
| 756 | |
| 757 | with self.assertRaisesRegex(TypeError, error_msg): |
| 758 | C().__init__(42) |
| 759 | |
| 760 | with self.assertRaisesRegex(TypeError, r'C\(\) takes no arguments'): |
| 761 | object.__new__(C, 42) |
| 762 | |
| 763 | with self.assertRaisesRegex(TypeError, error_msg): |
| 764 | object.__init__(C(), 42) |
| 765 | |
| 766 | # Class with both `__init__` & `__new__` method overridden |
| 767 | class D: |
| 768 | def __new__(cls, *args, **kwargs): |
| 769 | super().__new__(cls, *args, **kwargs) |
| 770 | def __init__(self, *args, **kwargs): |
| 771 | super().__init__(*args, **kwargs) |
| 772 | |
| 773 | error_msg = r'object.__new__\(\) takes exactly one argument \(the type to instantiate\)' |
| 774 | |
| 775 | with self.assertRaisesRegex(TypeError, error_msg): |
| 776 | D(42) |
| 777 | |
| 778 | with self.assertRaisesRegex(TypeError, error_msg): |
| 779 | D.__new__(D, 42) |
| 780 | |
| 781 | with self.assertRaisesRegex(TypeError, error_msg): |
| 782 | object.__new__(D, 42) |
| 783 | |
| 784 | # Class that only overrides __init__ |
| 785 | class E: |
| 786 | def __init__(self, *args, **kwargs): |
| 787 | super().__init__(*args, **kwargs) |
| 788 | |
| 789 | error_msg = r'object.__init__\(\) takes exactly one argument \(the instance to initialize\)' |
| 790 | |
| 791 | with self.assertRaisesRegex(TypeError, error_msg): |
| 792 | E().__init__(42) |
| 793 | |
| 794 | with self.assertRaisesRegex(TypeError, error_msg): |
| 795 | object.__init__(E(), 42) |
| 796 | |
| 797 | def testClassWithExtCall(self): |
| 798 | class Meta(int): |