mirror of
https://github.com/python/cpython.git
synced 2025-08-30 21:48:47 +00:00
gh-96663: Add a better error message for __dict__-less classes setattr (#103232)
This commit is contained in:
parent
41ca164551
commit
cdeb1a6caa
4 changed files with 32 additions and 5 deletions
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue