Skip to content

Commit 9002da6

Browse files
Fix typing
1 parent 0cd1459 commit 9002da6

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed
Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from functools import wraps
22

3-
from concurrent.futures import ThreadPoolExecutor
3+
from concurrent.futures import ThreadPoolExecutor, Future
44

55
import sentry_sdk
66
from sentry_sdk.integrations import Integration
@@ -9,8 +9,9 @@
99
from typing import TYPE_CHECKING
1010

1111
if TYPE_CHECKING:
12-
from typing import Any
13-
from typing import Callable
12+
from typing import Any, Callable, TypeVar
13+
14+
T = TypeVar("T", bound=Any)
1415

1516

1617
class ConcurrentIntegration(Integration):
@@ -23,24 +24,31 @@ def __init__(self, record_exceptions_on_futures=True):
2324
@staticmethod
2425
def setup_once():
2526
# type: () -> None
26-
old_submit = ThreadPoolExecutor.submit
27+
ThreadPoolExecutor.submit = _wrap_submit_call(ThreadPoolExecutor.submit)
28+
29+
30+
def _wrap_submit_call(func):
31+
# type: (Any) -> Any
32+
"""
33+
Wrap task call with a try catch to get exceptions.
34+
"""
2735

28-
@wraps(old_submit)
29-
def sentry_submit(self, fn, *args, **kwargs):
30-
# type: (ThreadPoolExecutor, Callable, *Any, **Any) -> Any
31-
integration = sentry_sdk.get_client().get_integration(ConcurrentIntegration)
32-
if integration is None:
33-
return old_submit(self, fn, *args, **kwargs)
36+
@wraps(func)
37+
def sentry_submit(self, fn, *args, **kwargs):
38+
# type: (ThreadPoolExecutor, Callable[..., T], *Any, **Any) -> Future[T]
39+
integration = sentry_sdk.get_client().get_integration(ConcurrentIntegration)
40+
if integration is None:
41+
return func(self, fn, *args, **kwargs)
3442

35-
isolation_scope = sentry_sdk.get_isolation_scope().fork()
36-
current_scope = sentry_sdk.get_current_scope().fork()
43+
isolation_scope = sentry_sdk.get_isolation_scope().fork()
44+
current_scope = sentry_sdk.get_current_scope().fork()
3745

38-
def wrapped_fn(*args, **kwargs):
39-
# type: (*Any, **Any) -> Any
40-
with use_isolation_scope(isolation_scope):
41-
with use_scope(current_scope):
42-
return fn(*args, **kwargs)
46+
def wrapped_fn(*args, **kwargs):
47+
# type: (*Any, **Any) -> Any
48+
with use_isolation_scope(isolation_scope):
49+
with use_scope(current_scope):
50+
return fn(*args, **kwargs)
4351

44-
return old_submit(self, wrapped_fn, *args, **kwargs)
52+
return func(self, wrapped_fn, *args, **kwargs)
4553

46-
ThreadPoolExecutor.submit = sentry_submit
54+
return sentry_submit

0 commit comments

Comments
 (0)