From bcfdbc634c47b0f4cb463c3117e2d6d301063215 Mon Sep 17 00:00:00 2001 From: Alexander Alderman Webb Date: Wed, 17 Dec 2025 13:38:13 +0100 Subject: [PATCH] fix(grpc): Gate third-party imports --- sentry_sdk/integrations/grpc/__init__.py | 29 +++++++++++++--------- sentry_sdk/integrations/grpc/aio/client.py | 24 ++++++++++-------- tests/test_shadowed_module.py | 1 - 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/sentry_sdk/integrations/grpc/__init__.py b/sentry_sdk/integrations/grpc/__init__.py index bc4025b8b3..b6641163a9 100644 --- a/sentry_sdk/integrations/grpc/__init__.py +++ b/sentry_sdk/integrations/grpc/__init__.py @@ -1,25 +1,30 @@ from functools import wraps -import grpc -from grpc import Channel, Server, intercept_channel -from grpc.aio import Channel as AsyncChannel -from grpc.aio import Server as AsyncServer - from sentry_sdk.integrations import Integration from sentry_sdk.utils import parse_version +from sentry_sdk.integrations import DidNotEnable from .client import ClientInterceptor from .server import ServerInterceptor -from .aio.server import ServerInterceptor as AsyncServerInterceptor -from .aio.client import ( - SentryUnaryUnaryClientInterceptor as AsyncUnaryUnaryClientInterceptor, -) -from .aio.client import ( - SentryUnaryStreamClientInterceptor as AsyncUnaryStreamClientIntercetor, -) from typing import TYPE_CHECKING, Any, Optional, Sequence +try: + import grpc + from grpc import Channel, Server, intercept_channel + from grpc.aio import Channel as AsyncChannel + from grpc.aio import Server as AsyncServer + + from .aio.server import ServerInterceptor as AsyncServerInterceptor + from .aio.client import ( + SentryUnaryUnaryClientInterceptor as AsyncUnaryUnaryClientInterceptor, + ) + from .aio.client import ( + SentryUnaryStreamClientInterceptor as AsyncUnaryStreamClientIntercetor, + ) +except ImportError: + raise DidNotEnable("grpcio is not installed.") + # Hack to get new Python features working in older versions # without introducing a hard dependency on `typing_extensions` # from: https://stackoverflow.com/a/71944042/300572 diff --git a/sentry_sdk/integrations/grpc/aio/client.py b/sentry_sdk/integrations/grpc/aio/client.py index 9ff27d1824..2edad83aff 100644 --- a/sentry_sdk/integrations/grpc/aio/client.py +++ b/sentry_sdk/integrations/grpc/aio/client.py @@ -1,19 +1,23 @@ from typing import Callable, Union, AsyncIterable, Any -from grpc.aio import ( - UnaryUnaryClientInterceptor, - UnaryStreamClientInterceptor, - ClientCallDetails, - UnaryUnaryCall, - UnaryStreamCall, - Metadata, -) -from google.protobuf.message import Message - import sentry_sdk from sentry_sdk.consts import OP +from sentry_sdk.integrations import DidNotEnable from sentry_sdk.integrations.grpc.consts import SPAN_ORIGIN +try: + from grpc.aio import ( + UnaryUnaryClientInterceptor, + UnaryStreamClientInterceptor, + ClientCallDetails, + UnaryUnaryCall, + UnaryStreamCall, + Metadata, + ) + from google.protobuf.message import Message +except ImportError: + raise DidNotEnable("grpcio is not installed") + class ClientInterceptor: @staticmethod diff --git a/tests/test_shadowed_module.py b/tests/test_shadowed_module.py index e1171dd103..315ba291cd 100644 --- a/tests/test_shadowed_module.py +++ b/tests/test_shadowed_module.py @@ -30,7 +30,6 @@ def pytest_generate_tests(metafunc): submodule_names - { "clickhouse_driver", - "grpc", "litellm", "opentelemetry", "pure_eval",