Skip to content

Instantly share code, notes, and snippets.

@1st1
Created January 30, 2014 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1st1/8703533 to your computer and use it in GitHub Desktop.
Save 1st1/8703533 to your computer and use it in GitHub Desktop.
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):
'aaa'
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(B.foo)) == '(self, a)'
print('ok')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment