Skip to content

Commit bc7834c

Browse files
ref: Move inline imports to the top level where possible
16 of the inline imports that PLC0415 flags don't need to be inline: stdlib modules that are already loaded on import sentry_sdk, internal modules with no circular import, names that can join an existing module-level import, and third-party imports from packages that the integration already imports at the top. Co-Authored-By: Ivana Kellyer <131587164+sentrivana@users.noreply.github.com>
1 parent 2b69972 commit bc7834c

11 files changed

Lines changed: 19 additions & 40 deletions

File tree

‎sentry_sdk/_compat.py‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import sys
22
from typing import TYPE_CHECKING
3+
from warnings import warn
4+
5+
from sentry_sdk.consts import FALSE_VALUES
36

47
if TYPE_CHECKING:
58
from typing import Any, TypeVar
@@ -39,8 +42,6 @@ def check_uwsgi_thread_support() -> bool:
3942
except ImportError:
4043
return True
4144

42-
from sentry_sdk.consts import FALSE_VALUES # noqa: PLC0415
43-
4445
def enabled(option: str) -> bool:
4546
value = opt.get(option, False)
4647
if isinstance(value, bool):
@@ -61,8 +62,6 @@ def enabled(option: str) -> bool:
6162
lazy_mode = enabled("lazy-apps") or enabled("lazy")
6263

6364
if lazy_mode and not threads_enabled:
64-
from warnings import warn # noqa: PLC0415
65-
6665
warn(
6766
Warning(
6867
"IMPORTANT: "
@@ -75,8 +74,6 @@ def enabled(option: str) -> bool:
7574
return False
7675

7776
elif not lazy_mode and (not threads_enabled or not fork_hooks_on):
78-
from warnings import warn # noqa: PLC0415
79-
8077
warn(
8178
Warning(
8279
"IMPORTANT: "

‎sentry_sdk/consts.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import inspect
12
import itertools
23
from enum import Enum
34
from typing import TYPE_CHECKING
@@ -1912,8 +1913,6 @@ def __init__(
19121913

19131914

19141915
def _get_default_options() -> "dict[str, Any]":
1915-
import inspect # noqa: PLC0415
1916-
19171916
a = inspect.getfullargspec(ClientConstructor.__init__)
19181917
defaults = a.defaults or ()
19191918
kwonlydefaults = a.kwonlydefaults or {}

‎sentry_sdk/integrations/__init__.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from abc import ABC, abstractmethod
2+
from importlib import import_module
23
from itertools import zip_longest
34
from threading import Lock
45
from typing import TYPE_CHECKING
@@ -30,8 +31,6 @@ def iter_default_integrations(
3031
with_auto_enabling_integrations: bool,
3132
) -> "Iterator[Type[Integration]]":
3233
"""Returns an iterator of the default integration classes:"""
33-
from importlib import import_module # noqa: PLC0415
34-
3534
if with_auto_enabling_integrations:
3635
all_import_strings = integrations + auto_enabling_integrations
3736
else:

‎sentry_sdk/integrations/celery/__init__.py‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@
3737

3838

3939
try:
40-
from celery import __version__ as CELERY_VERSION # type: ignore
40+
import celery.app.trace as trace # type: ignore
41+
from billiard.pool import Worker # type: ignore
42+
from celery import Celery
43+
from celery import __version__ as CELERY_VERSION
4144
from celery.app.task import Task # type: ignore
4245
from celery.app.trace import task_has_custom
4346
from celery.exceptions import ( # type: ignore
@@ -497,8 +500,6 @@ def _inner(*args: "Any", **kwargs: "Any") -> "Any":
497500

498501

499502
def _patch_build_tracer() -> None:
500-
import celery.app.trace as trace # type: ignore # noqa: PLC0415
501-
502503
original_build_tracer = trace.build_tracer
503504

504505
def sentry_build_tracer(
@@ -527,16 +528,12 @@ def _patch_task_apply_async() -> None:
527528

528529

529530
def _patch_celery_send_task() -> None:
530-
from celery import Celery # noqa: PLC0415
531-
532531
Celery.send_task = _wrap_task_run(Celery.send_task)
533532

534533

535534
def _patch_worker_exit() -> None:
536535
# Need to flush queue before worker shutdown because a crashing worker will
537536
# call os._exit
538-
from billiard.pool import Worker # type: ignore # noqa: PLC0415
539-
540537
original_workloop = Worker.workloop
541538

542539
def sentry_workloop(*args: "Any", **kwargs: "Any") -> "Any":

‎sentry_sdk/integrations/grpc/__init__.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,6 @@ class GRPCIntegration(Integration):
175175

176176
@staticmethod
177177
def setup_once() -> None:
178-
import grpc # noqa: PLC0415
179-
180178
grpc.insecure_channel = _wrap_channel_sync(grpc.insecure_channel)
181179
grpc.secure_channel = _wrap_channel_sync(grpc.secure_channel)
182180
grpc.intercept_channel = _wrap_intercept_channel(grpc.intercept_channel)

‎sentry_sdk/integrations/pyramid.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
)
2222

2323
try:
24+
from pyramid import router
2425
from pyramid.httpexceptions import HTTPException
2526
from pyramid.request import Request
2627
except ImportError:
@@ -69,8 +70,6 @@ def __init__(self, transaction_style: str = "route_name") -> None:
6970

7071
@staticmethod
7172
def setup_once() -> None:
72-
from pyramid import router # noqa: PLC0415
73-
7473
old_call_view = router._call_view
7574

7675
@functools.wraps(old_call_view)

‎sentry_sdk/scope.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,6 @@ def get_global_scope(cls) -> "Scope":
378378
return _global_scope
379379

380380
def set_global_attributes(self) -> None:
381-
from sentry_sdk.client import SDK_INFO # noqa: PLC0415
382-
383381
self.set_attribute(SPANDATA.SENTRY_SDK_NAME, SDK_INFO["name"])
384382
self.set_attribute(SPANDATA.SENTRY_SDK_VERSION, SDK_INFO["version"])
385383

@@ -2253,7 +2251,7 @@ def should_send_default_pii() -> bool:
22532251

22542252

22552253
# Circular imports
2256-
from sentry_sdk.client import NonRecordingClient
2254+
from sentry_sdk.client import SDK_INFO, NonRecordingClient
22572255

22582256
if TYPE_CHECKING:
22592257
import sentry_sdk.client

‎sentry_sdk/traces.py‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@
3232
try_autostart_continuous_profiler,
3333
try_profile_lifecycle_trace_start,
3434
)
35-
from sentry_sdk.tracing_utils import Baggage
35+
from sentry_sdk.tracing_utils import (
36+
Baggage,
37+
create_streaming_span_decorator,
38+
has_span_streaming_enabled,
39+
)
3640
from sentry_sdk.utils import (
3741
capture_internal_exceptions,
3842
format_attribute,
@@ -174,8 +178,6 @@ def start_span(
174178
:return: The span that has been started.
175179
:rtype: StreamedSpan
176180
"""
177-
from sentry_sdk.tracing_utils import has_span_streaming_enabled # noqa: PLC0415
178-
179181
client = sentry_sdk.get_client()
180182
if client.is_active() and not has_span_streaming_enabled(client.options):
181183
warnings.warn(
@@ -864,10 +866,6 @@ def make_db_query(sql):
864866
# Function implementation
865867
pass
866868
"""
867-
from sentry_sdk.tracing_utils import ( # noqa: PLC0415
868-
create_streaming_span_decorator,
869-
)
870-
871869
decorator = create_streaming_span_decorator(
872870
name=name,
873871
attributes=attributes,

‎sentry_sdk/tracing.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,8 +1466,6 @@ def calculate_interest_rate(amount, rate, years):
14661466
# Function implementation
14671467
pass
14681468
"""
1469-
from sentry_sdk.tracing_utils import create_span_decorator # noqa: PLC0415
1470-
14711469
decorator = create_span_decorator(
14721470
op=op,
14731471
name=name,
@@ -1487,6 +1485,7 @@ def calculate_interest_rate(amount, rate, years):
14871485
Baggage,
14881486
EnvironHeaders,
14891487
_generate_sample_rand,
1488+
create_span_decorator,
14901489
extract_sentrytrace_data,
14911490
has_span_streaming_enabled,
14921491
has_tracing_enabled,

‎sentry_sdk/transport.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import urllib3
4444

4545
import sentry_sdk
46-
from sentry_sdk.consts import EndpointType
46+
from sentry_sdk.consts import VERSION, EndpointType
4747
from sentry_sdk.envelope import Envelope, Item, PayloadRef
4848
from sentry_sdk.utils import (
4949
Dsn,
@@ -217,8 +217,6 @@ class HttpTransportCore(Transport):
217217
TIMEOUT = 30 # seconds
218218

219219
def __init__(self: "Self", options: "Dict[str, Any]") -> None:
220-
from sentry_sdk.consts import VERSION # noqa: PLC0415
221-
222220
Transport.__init__(self, options)
223221
assert self.parsed_dsn is not None
224222
self.options: "Dict[str, Any]" = options

0 commit comments

Comments
 (0)