Skip to content

Commit d781b88

Browse files
fix(clickhouse): Guard against module shadowing (#5250)
Use explicit imports to prevent `AttributeError` when `clickhouse_driver` is shadowed. Follows up on #5140.
1 parent f988f84 commit d781b88

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

sentry_sdk/integrations/clickhouse_driver.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ def __getitem__(self, _):
3030

3131

3232
try:
33-
import clickhouse_driver # type: ignore[import-not-found]
33+
from clickhouse_driver import VERSION # type: ignore[import-not-found]
34+
from clickhouse_driver.client import Client # type: ignore[import-not-found]
35+
from clickhouse_driver.connection import Connection # type: ignore[import-not-found]
3436

3537
except ImportError:
3638
raise DidNotEnable("clickhouse-driver not installed.")
@@ -42,29 +44,23 @@ class ClickhouseDriverIntegration(Integration):
4244

4345
@staticmethod
4446
def setup_once() -> None:
45-
_check_minimum_version(ClickhouseDriverIntegration, clickhouse_driver.VERSION)
47+
_check_minimum_version(ClickhouseDriverIntegration, VERSION)
4648

4749
# Every query is done using the Connection's `send_query` function
48-
clickhouse_driver.connection.Connection.send_query = _wrap_start(
49-
clickhouse_driver.connection.Connection.send_query
50-
)
50+
Connection.send_query = _wrap_start(Connection.send_query)
5151

5252
# If the query contains parameters then the send_data function is used to send those parameters to clickhouse
5353
_wrap_send_data()
5454

5555
# Every query ends either with the Client's `receive_end_of_query` (no result expected)
5656
# or its `receive_result` (result expected)
57-
clickhouse_driver.client.Client.receive_end_of_query = _wrap_end(
58-
clickhouse_driver.client.Client.receive_end_of_query
59-
)
60-
if hasattr(clickhouse_driver.client.Client, "receive_end_of_insert_query"):
57+
Client.receive_end_of_query = _wrap_end(Client.receive_end_of_query)
58+
if hasattr(Client, "receive_end_of_insert_query"):
6159
# In 0.2.7, insert queries are handled separately via `receive_end_of_insert_query`
62-
clickhouse_driver.client.Client.receive_end_of_insert_query = _wrap_end(
63-
clickhouse_driver.client.Client.receive_end_of_insert_query
60+
Client.receive_end_of_insert_query = _wrap_end(
61+
Client.receive_end_of_insert_query
6462
)
65-
clickhouse_driver.client.Client.receive_result = _wrap_end(
66-
clickhouse_driver.client.Client.receive_result
67-
)
63+
Client.receive_result = _wrap_end(Client.receive_result)
6864

6965

7066
P = ParamSpec("P")
@@ -128,7 +124,7 @@ def _inner_end(*args: "P.args", **kwargs: "P.kwargs") -> "T":
128124

129125

130126
def _wrap_send_data() -> None:
131-
original_send_data = clickhouse_driver.client.Client.send_data
127+
original_send_data = Client.send_data
132128

133129
def _inner_send_data( # type: ignore[no-untyped-def] # clickhouse-driver does not type send_data
134130
self, sample_block, data, types_check=False, columnar=False, *args, **kwargs
@@ -164,12 +160,10 @@ def wrapped_generator() -> "Iterator[Any]":
164160
self, sample_block, data, types_check, columnar, *args, **kwargs
165161
)
166162

167-
clickhouse_driver.client.Client.send_data = _inner_send_data
163+
Client.send_data = _inner_send_data
168164

169165

170-
def _set_db_data(
171-
span: "Span", connection: "clickhouse_driver.connection.Connection"
172-
) -> None:
166+
def _set_db_data(span: "Span", connection: "Connection") -> None:
173167
span.set_data(SPANDATA.DB_SYSTEM, "clickhouse")
174168
span.set_data(SPANDATA.SERVER_ADDRESS, connection.host)
175169
span.set_data(SPANDATA.SERVER_PORT, connection.port)

tests/test_shadowed_module.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def pytest_generate_tests(metafunc):
2929
# Temporarily skip some integrations
3030
submodule_names
3131
- {
32-
"clickhouse_driver",
3332
"litellm",
3433
"pure_eval",
3534
"ray",

0 commit comments

Comments
 (0)