Skip to content

Commit aad481d

Browse files
authored
Fix CI, adapt to new redis-py release (#4431)
Three new versions of things that need addressing: - pytest-asyncio 1.0 [removes](https://pytest-asyncio.readthedocs.io/en/latest/reference/changelog.html#id2) the deprecated `event_loop` fixture - mypy 1.16.0 finds some new issues - [redis 6.2.0](https://github.com/redis/redis-py/releases/tag/v6.2.0): - adds the `execution_strategy` abstraction to async cluster pipelines as well, so the commands are no longer accessible directly and need to be accessed via the `strategy` - renames the `_client` attribute on async cluster pipeline to `cluster_client`
1 parent 385c668 commit aad481d

File tree

6 files changed

+26
-24
lines changed

6 files changed

+26
-24
lines changed

sentry_sdk/_types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class SDKInfo(TypedDict):
170170
"contexts": dict[str, dict[str, object]],
171171
"dist": str,
172172
"duration": Optional[float],
173-
"environment": str,
173+
"environment": Optional[str],
174174
"errors": list[dict[str, Any]], # TODO: We can expand on this type
175175
"event_id": str,
176176
"exception": dict[
@@ -188,7 +188,7 @@ class SDKInfo(TypedDict):
188188
"monitor_slug": Optional[str],
189189
"platform": Literal["python"],
190190
"profile": object, # Should be sentry_sdk.profiler.Profile, but we can't import that here due to circular imports
191-
"release": str,
191+
"release": Optional[str],
192192
"request": dict[str, object],
193193
"sdk": Mapping[str, object],
194194
"server_name": str,

sentry_sdk/integrations/django/asgi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,9 @@ async def __acall__(self, *args, **kwargs):
237237
middleware_span = _check_middleware_span(old_method=f)
238238

239239
if middleware_span is None:
240-
return await f(*args, **kwargs)
240+
return await f(*args, **kwargs) # type: ignore
241241

242242
with middleware_span:
243-
return await f(*args, **kwargs)
243+
return await f(*args, **kwargs) # type: ignore
244244

245245
return SentryASGIMixin

sentry_sdk/integrations/redis/_async_common.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,21 @@ async def _sentry_execute(self, *args, **kwargs):
4141
origin=SPAN_ORIGIN,
4242
) as span:
4343
with capture_internal_exceptions():
44+
try:
45+
command_seq = self._execution_strategy._command_queue
46+
except AttributeError:
47+
if is_cluster:
48+
command_seq = self._command_stack
49+
else:
50+
command_seq = self.command_stack
51+
4452
set_db_data_fn(span, self)
4553
_set_pipeline_data(
4654
span,
4755
is_cluster,
4856
get_command_args_fn,
4957
False if is_cluster else self.is_transaction,
50-
self._command_stack if is_cluster else self.command_stack,
58+
command_seq,
5159
)
5260

5361
return await old_execute(self, *args, **kwargs)

sentry_sdk/integrations/redis/redis_cluster.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,19 @@ def _set_async_cluster_db_data(span, async_redis_cluster_instance):
3636
def _set_async_cluster_pipeline_db_data(span, async_redis_cluster_pipeline_instance):
3737
# type: (Span, AsyncClusterPipeline[Any]) -> None
3838
with capture_internal_exceptions():
39+
client = getattr(async_redis_cluster_pipeline_instance, "cluster_client", None)
40+
if client is None:
41+
# In older redis-py versions, the AsyncClusterPipeline had a `_client`
42+
# attr but it is private so potentially problematic and mypy does not
43+
# recognize it - see
44+
# https://github.com/redis/redis-py/blame/v5.0.0/redis/asyncio/cluster.py#L1386
45+
client = (
46+
async_redis_cluster_pipeline_instance._client # type: ignore[attr-defined]
47+
)
48+
3949
_set_async_cluster_db_data(
4050
span,
41-
# the AsyncClusterPipeline has always had a `_client` attr but it is private so potentially problematic and mypy
42-
# does not recognize it - see https://github.com/redis/redis-py/blame/v5.0.0/redis/asyncio/cluster.py#L1386
43-
async_redis_cluster_pipeline_instance._client, # type: ignore[attr-defined]
51+
client,
4452
)
4553

4654

sentry_sdk/integrations/tornado.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ async def sentry_execute_request_handler(self, *args, **kwargs):
7676
else:
7777

7878
@coroutine # type: ignore
79-
def sentry_execute_request_handler(self, *args, **kwargs): # type: ignore
79+
def sentry_execute_request_handler(self, *args, **kwargs):
8080
# type: (RequestHandler, *Any, **Any) -> Any
8181
with _handle_request_impl(self):
8282
result = yield from old_execute(self, *args, **kwargs)

tests/integrations/aiohttp/test_aiohttp.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66

77
import pytest
88

9-
try:
10-
import pytest_asyncio
11-
except ImportError:
12-
pytest_asyncio = None
13-
149
from aiohttp import web, ClientSession
1510
from aiohttp.client import ServerDisconnectedError
1611
from aiohttp.web_request import Request
@@ -27,14 +22,6 @@
2722
from tests.conftest import ApproxDict
2823

2924

30-
if pytest_asyncio is None:
31-
# `loop` was deprecated in `pytest-aiohttp`
32-
# in favor of `event_loop` from `pytest-asyncio`
33-
@pytest.fixture
34-
def event_loop(loop):
35-
yield loop
36-
37-
3825
@pytest.mark.asyncio
3926
async def test_basic(sentry_init, aiohttp_client, capture_events):
4027
sentry_init(integrations=[AioHttpIntegration()])
@@ -490,7 +477,7 @@ async def hello(request):
490477

491478
@pytest.mark.asyncio
492479
async def test_crumb_capture(
493-
sentry_init, aiohttp_raw_server, aiohttp_client, event_loop, capture_events
480+
sentry_init, aiohttp_raw_server, aiohttp_client, capture_events
494481
):
495482
def before_breadcrumb(crumb, hint):
496483
crumb["data"]["extra"] = "foo"
@@ -546,7 +533,6 @@ async def test_crumb_capture_client_error(
546533
sentry_init,
547534
aiohttp_raw_server,
548535
aiohttp_client,
549-
event_loop,
550536
capture_events,
551537
status_code,
552538
level,

0 commit comments

Comments
 (0)