Skip to content

Instantly share code, notes, and snippets.

Created January 30, 2014 06:18
What would you like to do?
import inspect
def inherit(*, base, docs=False, signature=False):
assert docs or signature
assert isinstance(base, type) # we can also support functions
def wrap(func):
base_func = getattr(base, func.__name__)
if docs:
doc = getattr(base_func, '__doc__', None)
assert doc
func.__doc__ = doc
if signature:
func.__signature__ = inspect.signature(base_func)
return func
return wrap
if __name__ == '__main__':
class A:
def foo(self, a):
return a
class B(A):
@inherit(base=A, docs=True, signature=True)
def foo(self, *args, **kwargs):
return super().foo(*args, **kwargs) + 32
assert B().foo.__doc__ == 'aaa'
assert str(inspect.signature(B().foo)) == '(a)'
assert str(inspect.signature( == '(self, a)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment