@@ -284,6 +284,66 @@ def spam(self):
284284 Foo .spam .__doc__ ,
285285 "spam wrapped in property subclass" )
286286
287+ @unittest .skipIf (sys .flags .optimize >= 2 ,
288+ "Docstrings are omitted with -O2 and above" )
289+ def test_docstring_copy2 (self ):
290+ """
291+ Property tries to provide the best docstring it finds for it's copies.
292+ If a user-provieded docstring is avaialble it's preserved in the copies.
293+ If no docsting is available during property creation, the property
294+ would utilze docstring from getter if available.
295+ """
296+ def getter1 (self ):
297+ return 1
298+ def getter2 (self ):
299+ """doc 2"""
300+ return 2
301+ def getter3 (self ):
302+ """doc 3"""
303+ return 3
304+
305+ # Case-1: user-provied doc is preserved in copies
306+ # of property with undocumented getter
307+ p = property (getter1 , None , None , "doc-A" )
308+
309+ p2 = p .getter (getter2 )
310+ self .assertEqual (p .__doc__ , "doc-A" )
311+ self .assertEqual (p2 .__doc__ , "doc-A" )
312+
313+ # Case-2: user-provied doc is preserved in copies
314+ # of property with documented getter
315+ p = property (getter2 , None , None , "doc-A" )
316+
317+ p2 = p .getter (getter3 )
318+ self .assertEqual (p .__doc__ , "doc-A" )
319+ self .assertEqual (p2 .__doc__ , "doc-A" )
320+
321+ # Case-3: with no user-provied doc new getter doc
322+ # takes precendence
323+ p = property (getter2 , None , None , None )
324+
325+ p2 = p .getter (getter3 )
326+ self .assertEqual (p .__doc__ , "doc 2" )
327+ self .assertEqual (p2 .__doc__ , "doc 3" )
328+
329+ # Case-4: A user-provied doc is assigned after property construction
330+ # with documented getter. The doc IS NOT preserved.
331+ # It's an odd behaviour, but it's a strange enough
332+ # use case with no easy solution.
333+ p = property (getter2 , None , None , None )
334+ p .__doc__ = "user"
335+ p2 = p .getter (getter3 )
336+ self .assertEqual (p .__doc__ , "user" )
337+ self .assertEqual (p2 .__doc__ , "doc 3" )
338+
339+ # Case-5: A user-provied doc is assigned after property construction
340+ # with UNdocumented getter. The doc IS preserved.
341+ p = property (getter1 , None , None , None )
342+ p .__doc__ = "user"
343+ p2 = p .getter (getter2 )
344+ self .assertEqual (p .__doc__ , "user" )
345+ self .assertEqual (p2 .__doc__ , "user" )
346+
287347 @unittest .skipIf (sys .flags .optimize >= 2 ,
288348 "Docstrings are omitted with -O2 and above" )
289349 def test_property_setter_copies_getter_docstring (self ):
0 commit comments