Skip to content

Commit 2f05ccb

Browse files
ref(api): Improve sentry_sdk.trace type hints (#2633)
Type hints for sentry_sdk.trace decorator function now indicate that the decorator returns a function with the same signature as it was called with. Previously, the type hints indicated that the decorator could return Any, which caused users to lose type hints for decorated functions. * Improve `sentry_sdk.trace` type hints * Add overloads for None case * Fix typing when `trace` called with `None` Fixes GH-2460
1 parent fe1f01b commit 2f05ccb

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

sentry_sdk/tracing.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,20 @@
1414
if TYPE_CHECKING:
1515
import typing
1616

17+
from collections.abc import Callable
1718
from typing import Any
1819
from typing import Dict
1920
from typing import Iterator
2021
from typing import List
2122
from typing import Optional
23+
from typing import overload
24+
from typing import ParamSpec
2225
from typing import Tuple
2326
from typing import Union
27+
from typing import TypeVar
28+
29+
P = ParamSpec("P")
30+
R = TypeVar("R")
2431

2532
import sentry_sdk.profiler
2633
from sentry_sdk._types import Event, MeasurementUnit, SamplingContext
@@ -983,8 +990,21 @@ def _set_initial_sampling_decision(self, sampling_context):
983990
pass
984991

985992

993+
if TYPE_CHECKING:
994+
995+
@overload
996+
def trace(func=None):
997+
# type: (None) -> Callable[[Callable[P, R]], Callable[P, R]]
998+
pass
999+
1000+
@overload
1001+
def trace(func):
1002+
# type: (Callable[P, R]) -> Callable[P, R]
1003+
pass
1004+
1005+
9861006
def trace(func=None):
987-
# type: (Any) -> Any
1007+
# type: (Optional[Callable[P, R]]) -> Union[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]
9881008
"""
9891009
Decorator to start a child span under the existing current transaction.
9901010
If there is no current transaction, then nothing will be traced.

0 commit comments

Comments
 (0)