summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_property.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_property.py')
-rw-r--r--Lib/test/test_property.py61
1 files changed, 56 insertions, 5 deletions
diff --git a/Lib/test/test_property.py b/Lib/test/test_property.py
index d4bdf50..45aa9e5 100644
--- a/Lib/test/test_property.py
+++ b/Lib/test/test_property.py
@@ -246,16 +246,67 @@ class PropertySubSlots(property):
class PropertySubclassTests(unittest.TestCase):
def test_slots_docstring_copy_exception(self):
- try:
+ # A special case error that we preserve despite the GH-98963 behavior
+ # that would otherwise silently ignore this error.
+ # This came from commit b18500d39d791c879e9904ebac293402b4a7cd34
+ # as part of https://bugs.python.org/issue5890 which allowed docs to
+ # be set via property subclasses in the first place.
+ with self.assertRaises(AttributeError):
class Foo(object):
@PropertySubSlots
def spam(self):
"""Trying to copy this docstring will raise an exception"""
return 1
- except AttributeError:
- pass
- else:
- raise Exception("AttributeError not raised")
+
+ def test_property_with_slots_no_docstring(self):
+ # https://github.com/python/cpython/issues/98963#issuecomment-1574413319
+ class slotted_prop(property):
+ __slots__ = ("foo",)
+
+ p = slotted_prop() # no AttributeError
+ self.assertIsNone(getattr(p, "__doc__", None))
+
+ def undocumented_getter():
+ return 4
+
+ p = slotted_prop(undocumented_getter) # New in 3.12: no AttributeError
+ self.assertIsNone(getattr(p, "__doc__", None))
+
+ @unittest.skipIf(sys.flags.optimize >= 2,
+ "Docstrings are omitted with -O2 and above")
+ def test_property_with_slots_docstring_silently_dropped(self):
+ # https://github.com/python/cpython/issues/98963#issuecomment-1574413319
+ class slotted_prop(property):
+ __slots__ = ("foo",)
+
+ p = slotted_prop(doc="what's up") # no AttributeError
+ self.assertIsNone(p.__doc__)
+
+ def documented_getter():
+ """getter doc."""
+ return 4
+
+ # Historical behavior: A docstring from a getter always raises.
+ # (matches test_slots_docstring_copy_exception above).
+ with self.assertRaises(AttributeError):
+ p = slotted_prop(documented_getter)
+
+ @unittest.skipIf(sys.flags.optimize >= 2,
+ "Docstrings are omitted with -O2 and above")
+ def test_property_with_slots_and_doc_slot_docstring_present(self):
+ # https://github.com/python/cpython/issues/98963#issuecomment-1574413319
+ class slotted_prop(property):
+ __slots__ = ("foo", "__doc__")
+
+ p = slotted_prop(doc="what's up")
+ self.assertEqual("what's up", p.__doc__) # new in 3.12: This gets set.
+
+ def documented_getter():
+ """what's up getter doc?"""
+ return 4
+
+ p = slotted_prop(documented_getter)
+ self.assertEqual("what's up getter doc?", p.__doc__)
@unittest.skipIf(sys.flags.optimize >= 2,
"Docstrings are omitted with -O2 and above")