mirror of
https://github.com/python/cpython.git
synced 2025-08-23 10:16:01 +00:00
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:
parent
43b135f94e
commit
a95138b2c5
4 changed files with 62 additions and 5 deletions
|
@ -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
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue