feat(celery): preserve original signature on apply_async wrapper for autospec compatibility#6400
Draft
JVenberg wants to merge 1 commit into
Draft
Conversation
`_wrap_task_run` replaces `Task.apply_async` (and `Celery.send_task`) with a wrapper declared as `def apply_async(*args, **kwargs)`. Without an explicit `__signature__`, tools that introspect the wrapper but do not follow `__wrapped__` (e.g. `inspect.getcallargs`) see the wrapper's bare `(*args, **kwargs)` instead of the wrapped function's parameter names. Setting `wrapper.__signature__ = inspect.signature(f)` keeps the original signature visible regardless of how callers inspect the wrapper. This mirrors PR getsentry#3178, which applied the same fix to the `sentry_sdk.tracing.trace` decorator.
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.
Summary
_wrap_task_runreplacesTask.apply_async(andCelery.send_task) with a wrapper declared as:functools.wrapscopies__wrapped__onto the wrapper, soinspect.signatureresolves through it. But other introspection tools that do not follow__wrapped__(e.g.inspect.getcallargs, IDE call-arg analysis, and some autospec-style test doubles built on__code__rather thansignature) see the wrapper's bare(*args, **kwargs)and lose the original parameter names.This change adds:
after the wrapper is defined, making the wrapper signature-transparent to all introspection paths.
Precedent
PR #3178 (fix
@sentry_sdk.tracing.tracechanging function signature, fixing #3177) applied this exact pattern tofunc_with_tracinginsentry_sdk/tracing_utils.py. The_wrap_task_runwrapper has the same shape; this PR brings it in line.Reproduction
Test
Added
test_wrap_task_run_preserves_signatureintests/integrations/celery/test_celery.py. The test fails onmaster(seesargs/kwargsplaceholders) and passes with the fix (seesself,args,kwargsbound by name).Why this matters downstream
At Rover.com (rover.com, ~1M+ DAU pet-services marketplace), our test suite defaults every
mock.patchtoautospec=True, spec_set=True(1,046 test files use this convention). Withsentry-sdk1.x we carry aFixSentryCeleryIntegrationintegration that wraps the private_wrap_apply_asyncsymbol just to forcewrapper.__signature__ = inspect.signature(<*args, **kwargs callable>). The same symbol exists in 2.x as_wrap_task_run, with the same gap. If this PR lands, that Rover-side monkey-patch can be deleted entirely on the next upgrade.General Notes
Filed without a pre-existing GitHub issue per the contribution guidelines note; happy to open one and reference it if maintainers prefer. PR is opened as a draft per the contribution standards.