bpo-43857: Improve the AttributeError message when deleting a missing attribute (#25424)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
Géry Ogam 2022-05-05 15:37:26 +02:00 committed by GitHub
parent 43b135f94e
commit a95138b2c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 62 additions and 5 deletions

View file

@ -611,6 +611,49 @@ class ClassTests(unittest.TestCase):
with self.assertRaises(TypeError):
type.__setattr__(A, b'x', None)
def testTypeAttributeAccessErrorMessages(self):
class A:
pass
error_msg = "type object 'A' has no attribute 'x'"
with self.assertRaisesRegex(AttributeError, error_msg):
A.x
with self.assertRaisesRegex(AttributeError, error_msg):
del A.x
def testObjectAttributeAccessErrorMessages(self):
class A:
pass
class B:
y = 0
__slots__ = ('z',)
error_msg = "'A' object has no attribute 'x'"
with self.assertRaisesRegex(AttributeError, error_msg):
A().x
with self.assertRaisesRegex(AttributeError, error_msg):
del A().x
error_msg = "'B' object has no attribute 'x'"
with self.assertRaisesRegex(AttributeError, error_msg):
B().x
with self.assertRaisesRegex(AttributeError, error_msg):
del B().x
with self.assertRaisesRegex(AttributeError, error_msg):
B().x = 0
error_msg = "'B' object attribute 'y' is read-only"
with self.assertRaisesRegex(AttributeError, error_msg):
del B().y
with self.assertRaisesRegex(AttributeError, error_msg):
B().y = 0
error_msg = 'z'
with self.assertRaisesRegex(AttributeError, error_msg):
B().z
with self.assertRaisesRegex(AttributeError, error_msg):
del B().z
def testConstructorErrorMessages(self):
# bpo-31506: Improves the error message logic for object_new & object_init