Skip to content

Commit 2b69972

Browse files
ci(ruff): Enable PLC0415 to flag inline imports in the SDK
AGENTS.md asks for top-level imports. Existing inline imports get a noqa comment, so a new one has to be marked explicitly. Tests are excluded. Co-Authored-By: Ivana Kellyer <131587164+sentrivana@users.noreply.github.com>
1 parent ae73d8c commit 2b69972

44 files changed

Lines changed: 109 additions & 105 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎pyproject.toml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ select = [
301301
"I",
302302
# Note: B and N rules are NOT enabled by default in flake8
303303
# They were only active through the plugins, which may not have been fully enabled
304+
"PLC0415", # Import not at the top level of the file
304305
]
305306

306307
# Use ONLY the same ignores as the original flake8 config + compatibility for this codebase
@@ -317,6 +318,9 @@ ignore = [
317318
"E721", # Use isinstance instead of type() == - existing pattern in this codebase
318319
]
319320

321+
[tool.ruff.lint.per-file-ignores]
322+
"tests/**" = ["PLC0415"]
323+
320324
[tool.ruff.format]
321325
# ruff format already excludes the same files as specified in extend-exclude
322326
# Ensure Python 3.7 compatibility - avoid using Python 3.9+ syntax features

‎sentry_sdk/_compat.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ def check_uwsgi_thread_support() -> bool:
3535
# --py-call-uwsgi-fork-hooks to force uWSGI to run regular cpython
3636
# after-fork hooks that take care of cleaning up stale thread data.
3737
try:
38-
from uwsgi import opt # type: ignore
38+
from uwsgi import opt # type: ignore # noqa: PLC0415
3939
except ImportError:
4040
return True
4141

42-
from sentry_sdk.consts import FALSE_VALUES
42+
from sentry_sdk.consts import FALSE_VALUES # noqa: PLC0415
4343

4444
def enabled(option: str) -> bool:
4545
value = opt.get(option, False)
@@ -61,7 +61,7 @@ def enabled(option: str) -> bool:
6161
lazy_mode = enabled("lazy-apps") or enabled("lazy")
6262

6363
if lazy_mode and not threads_enabled:
64-
from warnings import warn
64+
from warnings import warn # noqa: PLC0415
6565

6666
warn(
6767
Warning(
@@ -75,7 +75,7 @@ def enabled(option: str) -> bool:
7575
return False
7676

7777
elif not lazy_mode and (not threads_enabled or not fork_hooks_on):
78-
from warnings import warn
78+
from warnings import warn # noqa: PLC0415
7979

8080
warn(
8181
Warning(

‎sentry_sdk/client.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ def _record_lost_event(
653653

654654
# Setup Spotlight before creating batchers so _capture_envelope can use it.
655655
# setup_spotlight handles all config/env var resolution per the SDK spec.
656-
from sentry_sdk.spotlight import setup_spotlight
656+
from sentry_sdk.spotlight import setup_spotlight # noqa: PLC0415
657657

658658
self.spotlight = setup_spotlight(self.options)
659659
if self.spotlight is not None and not self.options["dsn"]:

‎sentry_sdk/consts.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1912,7 +1912,7 @@ def __init__(
19121912

19131913

19141914
def _get_default_options() -> "dict[str, Any]":
1915-
import inspect
1915+
import inspect # noqa: PLC0415
19161916

19171917
a = inspect.getfullargspec(ClientConstructor.__init__)
19181918
defaults = a.defaults or ()

‎sentry_sdk/integrations/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def iter_default_integrations(
3030
with_auto_enabling_integrations: bool,
3131
) -> "Iterator[Type[Integration]]":
3232
"""Returns an iterator of the default integration classes:"""
33-
from importlib import import_module
33+
from importlib import import_module # noqa: PLC0415
3434

3535
if with_auto_enabling_integrations:
3636
all_import_strings = integrations + auto_enabling_integrations

‎sentry_sdk/integrations/anthropic.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def setup_once() -> None:
281281
AsyncMessageStream.close = _wrap_async_close(AsyncMessageStream.close) # type: ignore[method-assign]
282282

283283
try:
284-
from functools import cached_property
284+
from functools import cached_property # noqa: PLC0415
285285
except ImportError:
286286
return
287287

‎sentry_sdk/integrations/beam.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class BeamIntegration(Integration):
3232

3333
@staticmethod
3434
def setup_once() -> None:
35-
from apache_beam.transforms.core import DoFn, ParDo # type: ignore
35+
from apache_beam.transforms.core import DoFn, ParDo # type: ignore # noqa: PLC0415
3636

3737
ignore_logger("root")
3838
ignore_logger("bundle_processor.create")
@@ -94,11 +94,11 @@ def _inspect(self: "Any") -> "Any":
9494
# getfullargspec is deprecated in more recent beam versions and get_function_args_defaults
9595
# (which uses Signatures internally) should be used instead.
9696
try:
97-
from apache_beam.transforms.core import get_function_args_defaults
97+
from apache_beam.transforms.core import get_function_args_defaults # noqa: PLC0415
9898

9999
return get_function_args_defaults(process_func)
100100
except ImportError:
101-
from apache_beam.typehints.decorators import getfullargspec # type: ignore
101+
from apache_beam.typehints.decorators import getfullargspec # type: ignore # noqa: PLC0415
102102

103103
return getfullargspec(process_func)
104104

‎sentry_sdk/integrations/celery/__init__.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ def _inner(*args: "Any", **kwargs: "Any") -> "Any":
497497

498498

499499
def _patch_build_tracer() -> None:
500-
import celery.app.trace as trace # type: ignore
500+
import celery.app.trace as trace # type: ignore # noqa: PLC0415
501501

502502
original_build_tracer = trace.build_tracer
503503

@@ -527,15 +527,15 @@ def _patch_task_apply_async() -> None:
527527

528528

529529
def _patch_celery_send_task() -> None:
530-
from celery import Celery
530+
from celery import Celery # noqa: PLC0415
531531

532532
Celery.send_task = _wrap_task_run(Celery.send_task)
533533

534534

535535
def _patch_worker_exit() -> None:
536536
# Need to flush queue before worker shutdown because a crashing worker will
537537
# call os._exit
538-
from billiard.pool import Worker # type: ignore
538+
from billiard.pool import Worker # type: ignore # noqa: PLC0415
539539

540540
original_workloop = Worker.workloop
541541

‎sentry_sdk/integrations/celery/beat.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def _wrap_beat_scheduler(
188188
if already_patched:
189189
return original_function
190190

191-
from sentry_sdk.integrations.celery import CeleryIntegration
191+
from sentry_sdk.integrations.celery import CeleryIntegration # noqa: PLC0415
192192

193193
def sentry_patched_scheduler(*args: "Any", **kwargs: "Any") -> None:
194194
integration = sentry_sdk.get_client().get_integration(CeleryIntegration)

‎sentry_sdk/integrations/chalice.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def setup_once() -> None:
163163
if version < (1, 20):
164164
old_get_view_function_response = Chalice._get_view_function_response
165165
else:
166-
from chalice.app import RestAPIEventHandler
166+
from chalice.app import RestAPIEventHandler # noqa: PLC0415
167167

168168
old_get_view_function_response = (
169169
RestAPIEventHandler._get_view_function_response

0 commit comments

Comments
 (0)