The decorator proposed here can inherit docstring for methods, but not for properties and getters.
I tried to naively extend it, but it seems that the docstrings of the properties are read-only. Is there a way to inherit them?
import types def fix_docs(cls): for name, func in vars(cls).items(): if isinstance(func, (types.FunctionType, property)) and not func.__doc__: print func, 'needs doc' for parent in cls.__bases__: parfunc = getattr(parent, name, None) if parfunc and getattr(parfunc, '__doc__', None): func.__doc__ = parfunc.__doc__ break return cls class X(object): """ some doc """ angle = 10 """Not too steep.""" def please_implement(self): """ I have a very thorough documentation :return: """ raise NotImplementedError @property def speed(self): """ Current speed in knots/hour. :return: """ return 0 @speed.setter def speed(self, value): """ :param value: :return: """ pass @fix_docs class SpecialX(X): angle = 30 def please_implement(self): return True @property def speed(self): return 10 @speed.setter def speed(self, value): self.sp = value help(X.speed) help(X.angle) help(SpecialX.speed) help(SpecialX.ange)
It only bothers me
Traceback (most recent call last): <function please_implement at 0x036101B0> needs doc <property object at 0x035BE930> needs doc File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.2\helpers\pydev\pydevd.py", line 1556, in <module> globals = debugger.run(setup['file'], None, None, is_module) File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.2\helpers\pydev\pydevd.py", line 940, in run pydev_imports.execfile(file, globals, locals)
python python-decorators
RedX Jul 26 '16 at 22:34 2016-07-26 22:34
source share