feat: Preserve metadata on wrapped coroutines #5105
Draft
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.
Description
Copy metadata when wrapping coroutines in the patched
asynciotask factory. Uses thefunctools.update_wrapper()function that is also used internally byfunctools.wraps(). Unlike when wrapping functions, less metadata is available on coroutines, so_wrap_coroutine()is equivalent tofunctools.wraps()with fewer attributes copied.CPython copies
__name__and__qualname__from function to coroutine: https://github.com/python/cpython/blob/a8733cbc7328dbeeed50e82ef13abb197d511c46/Objects/genobject.c#L1410.As
functools.update_wrapper()checks for the existence of properties first, copying metadata will not result in anAttributeErrorif metadata is not available. See the docs https://docs.python.org/3/library/functools.html#functools.update_wrapperA small script to generate a task destroyed error.
Issues
The SDK no longer overwrites coroutine metadata included in destroyed errors as reported in #5072.
Reminders
tox -e linters.feat:,fix:,ref:,meta:)