Created
December 14, 2015 07:33
-
-
Save chtenb/1343468b3f6dd108b591 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
from inspect import signature | |
from functools import wraps | |
def dec(func): | |
def wrapper(*args, **kwargs): | |
return func(*args, **kwargs) | |
return wrapper | |
def dec2(func): | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
return func(*args, **kwargs) | |
return wrapper | |
def foo(a: int, b): | |
pass | |
print(signature(dec(foo))) | |
print(signature(dec2(foo))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As per python 3.7:
functools.wraps
preserves signature but only in appearance because help and signature follow the__wrapped__
attribute ; it does not raise correct errors in case of misuse. See this answer, you can usemakefun.wraps
to get a fully signature-preserving behaviour like indecorator
. A very interesting side effect is that all arguments are redirected into thekwargs
variable when possible (when not varpositional), which is really helpful for the wrapper implementation.