MCPcopy Index your code
hub / github.com/python/cpython / testObjectAttributeAccessErrorMessages

Method testObjectAttributeAccessErrorMessages

Lib/test/test_class.py:693–740  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

691 del A.x
692
693 def testObjectAttributeAccessErrorMessages(self):
694 class A:
695 pass
696 class B:
697 y = 0
698 __slots__ = ('z',)
699 class C:
700 __slots__ = ("y",)
701
702 def __setattr__(self, name, value) -> None:
703 if name == "z":
704 super().__setattr__("y", 1)
705 else:
706 super().__setattr__(name, value)
707
708 error_msg = "'A' object has no attribute 'x'"
709 with self.assertRaisesRegex(AttributeError, error_msg):
710 A().x
711 with self.assertRaisesRegex(AttributeError, error_msg):
712 del A().x
713
714 error_msg = "'B' object has no attribute 'x'"
715 with self.assertRaisesRegex(AttributeError, error_msg):
716 B().x
717 with self.assertRaisesRegex(AttributeError, error_msg):
718 del B().x
719 with self.assertRaisesRegex(
720 AttributeError,
721 "'B' object has no attribute 'x' and no __dict__ for setting new attributes"
722 ):
723 B().x = 0
724 with self.assertRaisesRegex(
725 AttributeError,
726 "'C' object has no attribute 'x'"
727 ):
728 C().x = 0
729
730 error_msg = "'B' object attribute 'y' is read-only"
731 with self.assertRaisesRegex(AttributeError, error_msg):
732 del B().y
733 with self.assertRaisesRegex(AttributeError, error_msg):
734 B().y = 0
735
736 error_msg = 'z'
737 with self.assertRaisesRegex(AttributeError, error_msg):
738 B().z
739 with self.assertRaisesRegex(AttributeError, error_msg):
740 del B().z
741
742 def testConstructorErrorMessages(self):
743 # bpo-31506: Improves the error message logic for object_new & object_init

Callers

nothing calls this directly

Calls 4

assertRaisesRegexMethod · 0.80
AClass · 0.70
BClass · 0.70
CClass · 0.70

Tested by

no test coverage detected