Skip to content

Commit 0339776

Browse files
brettlangdonzemekrachelyangdog
committed
fix(tracing): ensure tracer.wrap preserves decorated function’s type (#13906)
This change reverts a regression introduced in `@tracer.wrap()` causes type checkers to infer the return type of decorated functions as `Any` instead of the actual return type. Addresses #13637 - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) --------- Co-authored-by: Joseph Zemek <jazemek@gmail.com> Co-authored-by: Rachel Yang <rachel.yang@datadoghq.com> Co-authored-by: Brett Langdon <brett.langdon@datadoghq.com>
1 parent 1f8c272 commit 0339776

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

ddtrace/_trace/tracer.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
from os import getpid
99
from threading import RLock
1010
from typing import TYPE_CHECKING
11-
from typing import Any
1211
from typing import Callable
1312
from typing import Dict
1413
from typing import List
1514
from typing import Optional
1615
from typing import Tuple
16+
from typing import TypeVar
1717
from typing import Union
18+
from typing import cast
1819

1920
from ddtrace._hooks import Hooks
2021
from ddtrace._trace.context import Context
@@ -66,7 +67,7 @@
6667
log = get_logger(__name__)
6768

6869

69-
AnyCallable = Callable[..., Any]
70+
AnyCallable = TypeVar("AnyCallable", bound=Callable)
7071

7172
if TYPE_CHECKING:
7273
from ddtrace.appsec._processor import AppSecSpanProcessor
@@ -779,7 +780,7 @@ def func_wrapper(*args, **kwargs):
779780
return_value = yield from f(*args, **kwargs)
780781
return return_value
781782

782-
return func_wrapper
783+
return cast(AnyCallable, func_wrapper)
783784

784785
def _wrap_generator_async(
785786
self,
@@ -797,7 +798,7 @@ async def func_wrapper(*args, **kwargs):
797798
async for value in f(*args, **kwargs):
798799
yield value
799800

800-
return func_wrapper
801+
return cast(AnyCallable, func_wrapper)
801802

802803
def wrap(
803804
self,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
tracing: This fix resolves an issue where the ``@tracer.wrap()`` decorator failed to preserve the decorated function's return type, returning ``Any`` instead of the original return type.

0 commit comments

Comments
 (0)