diff options
author | James Hilton-Balfe <gobot1234yt@gmail.com> | 2023-07-22 01:24:26 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-21 17:24:26 -0700 |
commit | cdeb1a6caad5e3067f01d6058238803b8517f9de (patch) | |
tree | 952950c42ffd25901e8b9319120f5e57ab9507bd /Lib/test/test_class.py | |
parent | 41ca16455188db806bfc7037058e8ecff2755e6c (diff) | |
download | cpython-cdeb1a6caad5e3067f01d6058238803b8517f9de.tar.gz cpython-cdeb1a6caad5e3067f01d6058238803b8517f9de.zip |
gh-96663: Add a better error message for __dict__-less classes setattr (#103232)
Diffstat (limited to 'Lib/test/test_class.py')
-rw-r--r-- | Lib/test/test_class.py | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/Lib/test/test_class.py b/Lib/test/test_class.py index 894e0ca67de..bb35baea508 100644 --- a/Lib/test/test_class.py +++ b/Lib/test/test_class.py @@ -641,6 +641,14 @@ class ClassTests(unittest.TestCase): class B: y = 0 __slots__ = ('z',) + class C: + __slots__ = ("y",) + + def __setattr__(self, name, value) -> None: + if name == "z": + super().__setattr__("y", 1) + else: + super().__setattr__(name, value) error_msg = "'A' object has no attribute 'x'" with self.assertRaisesRegex(AttributeError, error_msg): @@ -653,8 +661,16 @@ class ClassTests(unittest.TestCase): B().x with self.assertRaisesRegex(AttributeError, error_msg): del B().x - with self.assertRaisesRegex(AttributeError, error_msg): + with self.assertRaisesRegex( + AttributeError, + "'B' object has no attribute 'x' and no __dict__ for setting new attributes" + ): B().x = 0 + with self.assertRaisesRegex( + AttributeError, + "'C' object has no attribute 'x'" + ): + C().x = 0 error_msg = "'B' object attribute 'y' is read-only" with self.assertRaisesRegex(AttributeError, error_msg): |