Created
January 30, 2014 06:18
-
-
Save 1st1/8703533 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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