Wrap async_to_sync with functools.update_wrapper#565
Open
patrickwehbe wants to merge 1 commit into
Open
Conversation
async_to_sync didn't copy the wrapped function's metadata onto the returned callable, so things like __name__ and __wrapped__ were missing. sync_to_async already does this in its __init__, and AsyncToSync.__get__ does it too, so the __init__ case was just inconsistent. Add the update_wrapper call in AsyncToSync.__init__ and a test that mirrors the existing sync_to_async one. Fixes django#500
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #500.
async_to_syncdoesn't copy the wrapped function's metadata onto the callable it returns, so attributes like__name__and__wrapped__are missing. People work around it by wrapping the result infunctools.wrapsthemselves (the issue shows this).sync_to_asyncalready callsfunctools.update_wrapper(self, func)in its__init__, andAsyncToSync.__get__does the same when used as a method decorator.AsyncToSync.__init__was the only one that didn't, so this was just an inconsistency.The fix is one line in
AsyncToSync.__init__. I also added a test next to the existingsync_to_asyncattribute test. Without the fix the new test fails withAttributeError: 'AsyncToSync' object has no attribute '__name__'; with it the name, docstring,__wrapped__and custom attributes all carry over.