Skip to content

Commit 256b5d6

Browse files
fix(grpc): Gate third-party imports (#5246)
1 parent c9f06b7 commit 256b5d6

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

sentry_sdk/integrations/grpc/__init__.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
from functools import wraps
22

3-
import grpc
4-
from grpc import Channel, Server, intercept_channel
5-
from grpc.aio import Channel as AsyncChannel
6-
from grpc.aio import Server as AsyncServer
7-
83
from sentry_sdk.integrations import Integration
94
from sentry_sdk.utils import parse_version
5+
from sentry_sdk.integrations import DidNotEnable
106

117
from .client import ClientInterceptor
128
from .server import ServerInterceptor
13-
from .aio.server import ServerInterceptor as AsyncServerInterceptor
14-
from .aio.client import (
15-
SentryUnaryUnaryClientInterceptor as AsyncUnaryUnaryClientInterceptor,
16-
)
17-
from .aio.client import (
18-
SentryUnaryStreamClientInterceptor as AsyncUnaryStreamClientIntercetor,
19-
)
209

2110
from typing import TYPE_CHECKING, Any, Optional, Sequence
2211

12+
try:
13+
import grpc
14+
from grpc import Channel, Server, intercept_channel
15+
from grpc.aio import Channel as AsyncChannel
16+
from grpc.aio import Server as AsyncServer
17+
18+
from .aio.server import ServerInterceptor as AsyncServerInterceptor
19+
from .aio.client import (
20+
SentryUnaryUnaryClientInterceptor as AsyncUnaryUnaryClientInterceptor,
21+
)
22+
from .aio.client import (
23+
SentryUnaryStreamClientInterceptor as AsyncUnaryStreamClientIntercetor,
24+
)
25+
except ImportError:
26+
raise DidNotEnable("grpcio is not installed.")
27+
2328
# Hack to get new Python features working in older versions
2429
# without introducing a hard dependency on `typing_extensions`
2530
# from: https://stackoverflow.com/a/71944042/300572

sentry_sdk/integrations/grpc/aio/client.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
from typing import Callable, Union, AsyncIterable, Any
22

3-
from grpc.aio import (
4-
UnaryUnaryClientInterceptor,
5-
UnaryStreamClientInterceptor,
6-
ClientCallDetails,
7-
UnaryUnaryCall,
8-
UnaryStreamCall,
9-
Metadata,
10-
)
11-
from google.protobuf.message import Message
12-
133
import sentry_sdk
144
from sentry_sdk.consts import OP
5+
from sentry_sdk.integrations import DidNotEnable
156
from sentry_sdk.integrations.grpc.consts import SPAN_ORIGIN
167

8+
try:
9+
from grpc.aio import (
10+
UnaryUnaryClientInterceptor,
11+
UnaryStreamClientInterceptor,
12+
ClientCallDetails,
13+
UnaryUnaryCall,
14+
UnaryStreamCall,
15+
Metadata,
16+
)
17+
from google.protobuf.message import Message
18+
except ImportError:
19+
raise DidNotEnable("grpcio is not installed")
20+
1721

1822
class ClientInterceptor:
1923
@staticmethod

tests/test_shadowed_module.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ def pytest_generate_tests(metafunc):
3030
submodule_names
3131
- {
3232
"clickhouse_driver",
33-
"grpc",
3433
"litellm",
3534
"opentelemetry",
3635
"pure_eval",

0 commit comments

Comments
 (0)