summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_class.py
diff options
context:
space:
mode:
authorGéry Ogam <gery.ogam@gmail.com>2022-05-05 13:37:26 (GMT)
committerGitHub <noreply@github.com>2022-05-05 13:37:26 (GMT)
commita95138b2c5a3ba3d9a1a635566e22e5843b6a45c (patch)
tree9df22636e46deb06e0c8897ed0d072c440c42a7b /Lib/test/test_class.py
parent43b135f94ebf3e6e84ddb0f75ed8510b96a610e4 (diff)
downloadcpython-a95138b2c5a3ba3d9a1a635566e22e5843b6a45c.zip
cpython-a95138b2c5a3ba3d9a1a635566e22e5843b6a45c.tar.gz
cpython-a95138b2c5a3ba3d9a1a635566e22e5843b6a45c.tar.bz2
bpo-43857: Improve the AttributeError message when deleting a missing attribute (#25424)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Diffstat (limited to 'Lib/test/test_class.py')
-rw-r--r--Lib/test/test_class.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/Lib/test/test_class.py b/Lib/test/test_class.py
index 7cf5e06..91c53b7 100644
--- a/Lib/test/test_class.py
+++ b/Lib/test/test_class.py
@@ -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