Update test_property from cpython 3.10.6
@@ -195,6 +195,37 @@ class PropertyTests(unittest.TestCase):
fake_prop.__init__('fget', 'fset', 'fdel', 'doc')
self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10)
+ @unittest.skipIf(sys.flags.optimize >= 2,
+ "Docstrings are omitted with -O2 and above")
+ def test_class_property(self):
+ class A:
+ @classmethod
+ @property
+ def __doc__(cls):
+ return 'A doc for %r' % cls.__name__
+ self.assertEqual(A.__doc__, "A doc for 'A'")
+
+ @unittest.skipIf(sys.flags.optimize >= 2,
+ "Docstrings are omitted with -O2 and above")
+ def test_class_property_override(self):
+ class A:
+ """First"""
+ @classmethod
+ @property
+ def __doc__(cls):
+ return 'Second'
+ self.assertEqual(A.__doc__, 'Second')
+
+ def test_property_set_name_incorrect_args(self):
+ p = property()
+
+ for i in (0, 1, 3):
+ with self.assertRaisesRegex(
+ TypeError,
+ fr'^__set_name__\(\) takes 2 positional arguments but {i} were given$'
+ ):
+ p.__set_name__(*([0] * i))
+
# Issue 5890: subclasses of property do not preserve method __doc__ strings
class PropertySub(property):
@@ -298,6 +329,46 @@ class PropertySubclassTests(unittest.TestCase):
self.assertEqual(Foo.spam.__doc__, "a new docstring")
+class _PropertyUnreachableAttribute:
+ msg_format = None
+ obj = None
+ cls = None
+
+ def _format_exc_msg(self, msg):
+ return self.msg_format.format(msg)
+
+ @classmethod
+ def setUpClass(cls):
+ cls.obj = cls.cls()
+
+ def test_get_property(self):
+ with self.assertRaisesRegex(AttributeError, self._format_exc_msg("unreadable attribute")):
+ self.obj.foo
+
+ def test_set_property(self):
+ with self.assertRaisesRegex(AttributeError, self._format_exc_msg("can't set attribute")):
+ self.obj.foo = None
+
+ def test_del_property(self):
+ with self.assertRaisesRegex(AttributeError, self._format_exc_msg("can't delete attribute")):
+ del self.obj.foo
+
+
+class PropertyUnreachableAttributeWithName(_PropertyUnreachableAttribute, unittest.TestCase):
+ msg_format = "^{} 'foo'$"
+
+ class cls:
+ foo = property()
+
+
+class PropertyUnreachableAttributeNoName(_PropertyUnreachableAttribute, unittest.TestCase):
+ msg_format = "^{}$"
+
+ class cls:
+ pass
+
+ cls.foo = property()
+
if __name__ == '__main__':
unittest.main()
