inheriting docstrings and mutable docstings for classes

On Thu, Jun 9, 2011 at 8:05 PM, Nick Coghlan <ncoghlan@gmail.com> wrote: oldschool OOP books, typically a specialization of a more general (ie. abstract) outer class. The multiple-inheritance case does make things a bit more sloppy, but then this problem has already been resolved by the BDFL via MRO, the same could probably apply with docstrings, with the user updating or using a blank docstring when that general rule doesn't work. In any case, I found myself wanting this auto-inheritance for easier testing with doctest. I don't want my subclasses to mess up invariants in my parent classes, and if the doctests were inherited this would be easy to check. Just my (late) 2 cents worth after examining the current python issues list. mark Sorry for any formatting problems, this is a forward after accidently replying only to ncoghlan.

On 25/08/12 10:10, Mark Adam wrote:
I have run into exactly that issue myself. But then I realised that this may not work in practice, and in fact could be outright misleading. The problem is that my class docstrings probably refer to the class by name: class Spam: def get(self, n): """Get n chunks of lovely spam. >>> Spam().get(4) 'Spam spam spam LOVELY SPAM!!!' """ class Ham(Spam): pass If Ham.get inherits the docstring, I may be fooled into thinking that I've tested Ham.get when all I've done is test Spam.get twice. A better solution to this use-case might be a class decorator which copies docstrings from superclasses (if they aren't explicitly set in the subclass). The decorator could optionally apply a bunch of string transformations to the docstrings: @copy_docstrings({'spam': 'ham'}, case_mangling=True) class Ham(Spam): pass -- Steven

On Fri, Aug 24, 2012 at 6:54 PM, Steven D'Aprano <steve@pearwood.info> wrote:
Yeah, see http://bugs.python.org/issue15731. -eric

On 25/08/12 10:10, Mark Adam wrote:
I have run into exactly that issue myself. But then I realised that this may not work in practice, and in fact could be outright misleading. The problem is that my class docstrings probably refer to the class by name: class Spam: def get(self, n): """Get n chunks of lovely spam. >>> Spam().get(4) 'Spam spam spam LOVELY SPAM!!!' """ class Ham(Spam): pass If Ham.get inherits the docstring, I may be fooled into thinking that I've tested Ham.get when all I've done is test Spam.get twice. A better solution to this use-case might be a class decorator which copies docstrings from superclasses (if they aren't explicitly set in the subclass). The decorator could optionally apply a bunch of string transformations to the docstrings: @copy_docstrings({'spam': 'ham'}, case_mangling=True) class Ham(Spam): pass -- Steven

On Fri, Aug 24, 2012 at 6:54 PM, Steven D'Aprano <steve@pearwood.info> wrote:
Yeah, see http://bugs.python.org/issue15731. -eric
participants (3)
-
Eric Snow
-
Mark Adam
-
Steven D'Aprano