-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
functools.update_wrapper copies all __dict__ attributes to the wrapper by default, which can be surprising when used on callable instances #105933
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Don't use There is a piece of code updating def update_wrapper(wrapper,
wrapped,
assigned = WRAPPER_ASSIGNMENTS,
updated = WRAPPER_UPDATES):
for attr in assigned:
try:
value = getattr(wrapped, attr)
except AttributeError:
pass
else:
setattr(wrapper, attr, value)
for attr in updated:
getattr(wrapper, attr).update(getattr(wrapped, attr, {}))
# Issue #17482: set __wrapped__ last so we don't inadvertently copy it
# from the wrapped function when updating __dict__
wrapper.__wrapped__ = wrapped
# Return the wrapper so this can be used as a decorator via partial()
return wrapper for attr in updated:
getattr(wrapper, attr).update(getattr(wrapped, attr, {})) |
The culprit here is the This does mean that the default behavior of This is easy to work around; just use This behavior of I do think the |
I updated the wording of the OP to clarify the actual issue. |
Uh oh!
There was an error while loading. Please reload this page.
Bug report
functools.update_wrapper
silentlyremove dataclass function wrappercopies all attributes from wrapped callable to wrapper, which can lead to surprising behavior. In this case thefun
attribute is copied, totally changing the behavior of the code:Repro:
Removing
__post_init__
fixes the issue.Your environment
Linux, 3.10.11
The text was updated successfully, but these errors were encountered: