1+ from __future__ import annotations
12import sentry_sdk
23from sentry_sdk .consts import OP , SPANDATA
34from sentry_sdk .integrations import _check_minimum_version , Integration , DidNotEnable
910 ensure_integration_enabled ,
1011)
1112
12- from typing import TYPE_CHECKING , cast , Any , Dict , TypeVar
13+ from typing import TYPE_CHECKING
1314
14- # Hack to get new Python features working in older versions
15- # without introducing a hard dependency on `typing_extensions`
16- # from: https://stackoverflow.com/a/71944042/300572
1715if TYPE_CHECKING :
18- from typing import ParamSpec , Callable
19- else :
20- # Fake ParamSpec
21- class ParamSpec :
22- def __init__ (self , _ ):
23- self .args = None
24- self .kwargs = None
16+ from typing import ParamSpec , Callable , Any , Dict , TypeVar
2517
26- # Callable[anything] will return None
27- class _Callable :
28- def __getitem__ (self , _ ):
29- return None
30-
31- # Make instances
32- Callable = _Callable ()
18+ P = ParamSpec ("P" )
19+ T = TypeVar ("T" )
3320
3421
3522try :
@@ -72,10 +59,6 @@ def setup_once() -> None:
7259 )
7360
7461
75- P = ParamSpec ("P" )
76- T = TypeVar ("T" )
77-
78-
7962def _wrap_start (f : Callable [P , T ]) -> Callable [P , T ]:
8063 @ensure_integration_enabled (ClickhouseDriverIntegration , f )
8164 def _inner (* args : P .args , ** kwargs : P .kwargs ) -> T :
@@ -93,8 +76,7 @@ def _inner(*args: P.args, **kwargs: P.kwargs) -> T:
9376
9477 connection ._sentry_span = span # type: ignore[attr-defined]
9578
96- data = _get_db_data (connection )
97- data = cast ("dict[str, Any]" , data )
79+ data : dict [str , Any ] = _get_db_data (connection )
9880 data ["db.query.text" ] = query
9981
10082 if query_id :
@@ -117,7 +99,11 @@ def _inner(*args: P.args, **kwargs: P.kwargs) -> T:
11799def _wrap_end (f : Callable [P , T ]) -> Callable [P , T ]:
118100 def _inner_end (* args : P .args , ** kwargs : P .kwargs ) -> T :
119101 res = f (* args , ** kwargs )
120- client = cast ("clickhouse_driver.client.Client" , args [0 ])
102+
103+ client = args [0 ]
104+ if not isinstance (client , clickhouse_driver .client .Client ):
105+ return res
106+
121107 connection = client .connection
122108
123109 span = getattr (connection , "_sentry_span" , None )
@@ -150,21 +136,25 @@ def _inner_end(*args: P.args, **kwargs: P.kwargs) -> T:
150136
151137def _wrap_send_data (f : Callable [P , T ]) -> Callable [P , T ]:
152138 def _inner_send_data (* args : P .args , ** kwargs : P .kwargs ) -> T :
153- client = cast ("clickhouse_driver.client.Client" , args [0 ])
139+ client = args [0 ]
140+ if not isinstance (client , clickhouse_driver .client .Client ):
141+ return f (* args , ** kwargs )
142+
154143 connection = client .connection
155- db_params_data = cast ("list[Any]" , args [2 ])
156144 span = getattr (connection , "_sentry_span" , None )
157145
158146 if span is not None :
159147 data = _get_db_data (connection )
160148 _set_on_span (span , data )
161149
162150 if should_send_default_pii ():
163- saved_db_data = getattr (
151+ saved_db_data : dict [ str , Any ] = getattr (
164152 connection , "_sentry_db_data" , {}
165- ) # type: dict[str, Any]
166- db_params = saved_db_data .get ("db.params" ) or [] # type: list[Any]
167- db_params .extend (db_params_data )
153+ )
154+ db_params : list [Any ] = saved_db_data .get ("db.params" ) or []
155+ db_params_data = args [2 ]
156+ if isinstance (db_params_data , list ):
157+ db_params .extend (db_params_data )
168158 saved_db_data ["db.params" ] = db_params
169159 span .set_attribute ("db.params" , _serialize_span_attribute (db_params ))
170160
0 commit comments