@@ -246,16 +246,67 @@ class PropertySubSlots(property):
246246class PropertySubclassTests (unittest .TestCase ):
247247
248248 def test_slots_docstring_copy_exception (self ):
249- try :
249+ # A special case error that we preserve despite the GH-98963 behavior
250+ # that would otherwise silently ignore this error.
251+ # This came from commit b18500d39d791c879e9904ebac293402b4a7cd34
252+ # as part of https://bugs.python.org/issue5890 which allowed docs to
253+ # be set via property subclasses in the first place.
254+ with self .assertRaises (AttributeError ):
250255 class Foo (object ):
251256 @PropertySubSlots
252257 def spam (self ):
253258 """Trying to copy this docstring will raise an exception"""
254259 return 1
255- except AttributeError :
256- pass
257- else :
258- raise Exception ("AttributeError not raised" )
260+
261+ def test_property_with_slots_no_docstring (self ):
262+ # https://github.com/python/cpython/issues/98963#issuecomment-1574413319
263+ class slotted_prop (property ):
264+ __slots__ = ("foo" ,)
265+
266+ p = slotted_prop () # no AttributeError
267+ self .assertIsNone (getattr (p , "__doc__" , None ))
268+
269+ def undocumented_getter ():
270+ return 4
271+
272+ p = slotted_prop (undocumented_getter ) # New in 3.12: no AttributeError
273+ self .assertIsNone (getattr (p , "__doc__" , None ))
274+
275+ @unittest .skipIf (sys .flags .optimize >= 2 ,
276+ "Docstrings are omitted with -O2 and above" )
277+ def test_property_with_slots_docstring_silently_dropped (self ):
278+ # https://github.com/python/cpython/issues/98963#issuecomment-1574413319
279+ class slotted_prop (property ):
280+ __slots__ = ("foo" ,)
281+
282+ p = slotted_prop (doc = "what's up" ) # no AttributeError
283+ self .assertIsNone (p .__doc__ )
284+
285+ def documented_getter ():
286+ """getter doc."""
287+ return 4
288+
289+ # Historical behavior: A docstring from a getter always raises.
290+ # (matches test_slots_docstring_copy_exception above).
291+ with self .assertRaises (AttributeError ):
292+ p = slotted_prop (documented_getter )
293+
294+ @unittest .skipIf (sys .flags .optimize >= 2 ,
295+ "Docstrings are omitted with -O2 and above" )
296+ def test_property_with_slots_and_doc_slot_docstring_present (self ):
297+ # https://github.com/python/cpython/issues/98963#issuecomment-1574413319
298+ class slotted_prop (property ):
299+ __slots__ = ("foo" , "__doc__" )
300+
301+ p = slotted_prop (doc = "what's up" )
302+ self .assertEqual ("what's up" , p .__doc__ ) # new in 3.12: This gets set.
303+
304+ def documented_getter ():
305+ """what's up getter doc?"""
306+ return 4
307+
308+ p = slotted_prop (documented_getter )
309+ self .assertEqual ("what's up getter doc?" , p .__doc__ )
259310
260311 @unittest .skipIf (sys .flags .optimize >= 2 ,
261312 "Docstrings are omitted with -O2 and above" )
0 commit comments