New submission from Thor Whalen <thorwhal...@gmail.com>:
# PROBLEM When using `functools.wraps`, the signature claims one set of defaults, but the (wrapped) function uses the original (wrappee) defaults. Why might that be the desirable default? # PROPOSED SOLUTION Adding '__defaults__', '__kwdefaults__' to `WRAPPER_ASSIGNMENTS` so that actual defaults can be consistent with signature defaults. # Code Demo ```python from functools import wraps from inspect import signature def g(a: float, b=10): return a * b def f(a: int, b=1): return a * b assert f(3) == 3 f = wraps(g)(f) assert str(signature(f)) == '(a: float, b=10)' # signature says that b defaults to 10 assert f.__defaults__ == (1,) # ... but it's a lie! assert f(3) == 3 != g(3) == 30 # ... and one that can lead to problems! ``` Why is this so? Because `functools.wraps` updates the `__signature__` (including annotations and defaults), `__annotations__`, but not `__defaults__`, which python apparently looks to in order to assign defaults. One solution is to politely ask wraps to include these defaults. ```python from functools import wraps, WRAPPER_ASSIGNMENTS, partial my_wraps = partial(wraps, assigned=(list(WRAPPER_ASSIGNMENTS) + ['__defaults__', '__kwdefaults__'])) def g(a: float, b=10): return a * b def f(a: int, b=1): return a * b assert f(3) == 3 f = my_wraps(g)(f) assert f(3) == 30 == g(3) assert f.__defaults__ == (10,) # ... because now got g defaults! ``` Wouldn't it be better to get this out of the box? When would I want the defaults that are actually used be different than those mentioned in the signature? <!-- Thanks for your contribution! Please read this comment in its entirety. It's quite important. # Pull Request title It should be in the following format: ``` bpo-NNNN: Summary of the changes made ``` Where: bpo-NNNN refers to the issue number in the https://bugs.python.org. Most PRs will require an issue number. Trivial changes, like fixing a typo, do not need an issue. # Backport Pull Request title If this is a backport PR (PR made against branches other than `master`), please ensure that the PR title is in the following format: ``` [X.Y] <title from the original PR> (GH-NNNN) ``` Where: [X.Y] is the branch name, e.g. [3.6]. GH-NNNN refers to the PR number from `master`. --> ---------- messages: 373254 nosy: Thor Whalen2 priority: normal severity: normal status: open title: Python `functools.wraps` doesn't deal with defaults correctly type: enhancement versions: Python 3.8 _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue41232> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com