Skip to content

ci(ruff): Enable PLC0415 to flag inline imports in the SDK - #8285

Merged
sentrivana merged 5 commits into
masterfrom
ci/ruff-inline-imports
Nov 16, 2026
Merged

sentrivana merged 5 commits into
masterfrom
ci/ruff-inline-imports

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Description

Turns on ruff's PLC0415, which flags imports that aren't at the top level of the file. This enforces the top-level-imports guideline in AGENTS.md, which reviewers currently check by hand. The rule covers sentry_sdk only; tests are excluded.

16 existing inline imports didn't need to be inline, so they're now at the top level or in an existing module-level import. The other 89 are intentional (optional deps, version checks, circular imports, Django) and get a # noqa: PLC0415 comment, so any new inline import needs the same explicit marker.

Each ruff-rule PR (#8282, #8283, #8285, #8286, #8287) targets master on its own. They all add a line to the same select list in pyproject.toml, so whichever merges first leaves the others with a small conflict there to resolve.

via @sentrivana.

--

View Session [Sentry]

@sentrivana

Copy link
Copy Markdown
Collaborator

@sentry-junior Can you see if we can move any of the existing inline imports to the top level? There are some that won't work for sure, but some might be movable.

@sentry-junior

sentry-junior Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Moved 16 of the 105 to the top level in 38a3fa3. 89 noqa: PLC0415 comments are left.

Moved

  • Stdlib modules that import sentry_sdk already loads: warnings.warn and FALSE_VALUES in _compat.py, inspect in consts.py, importlib.import_module in integrations/__init__.py, threading.local in utils.py (threading was already imported there).
  • Internal imports with no import cycle: VERSION in transport.py, plus the two tracing_utils imports in traces.py, which now join that file's existing top-level tracing_utils import.
  • Names added to an existing bottom-of-module "circular imports" block: SDK_INFO in scope.py and create_span_decorator in tracing.py.
  • Third-party imports where the integration already imports the same package at the top: celery.app.trace, Celery and billiard.pool.Worker in the celery integration, pyramid.router, and a duplicate import grpc that I removed. Moving the celery imports made one type: ignore unnecessary, so I removed it.

Left inline

  • 44 are inside try/except, guarding an optional dependency or a version-specific API.
  • 22 are internal imports that would create a cycle, such as DjangoIntegration and RedisIntegration imported from their own submodules, or serializer in utils.py. The spotlight import in client.py also stays: spotlight.py imports Django at module level, so moving it would make import sentry_sdk import Django.
  • 11 are Django imports in the Django integration. I didn't move them. My guess is they're inline because sentry_sdk.init runs inside settings.py, before Django is set up, but I haven't checked each one.
  • 9 are spark/beam imports in integrations that never import that package at the top.
  • 3 others: chalice.app.RestAPIEventHandler (only exists in chalice 1.20+), tiktoken (optional), and __main__ (GCP).

How I checked

  • Imported every sentry_sdk module in a fresh interpreter, before and after the change. The results are identical, and import sentry_sdk loads the same set of modules as before.
  • mypy, py3.14-common, and the latest grpc, pyramid and celery tox envs pass. The only celery failures are 3 beat tests that need a Redis server; they fail the same way on the base branch.

sentry-junior Bot and others added 2 commits November 16, 2026 09:54
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>
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>
@sentry-junior
sentry-junior Bot removed this pull request from stack #8284 November 16, 2026 10:03
@sentry-junior
sentry-junior Bot changed the base branch from ci/ruff-print to master November 16, 2026 10:03
@sentry-junior
sentry-junior Bot force-pushed the ci/ruff-inline-imports branch from 38a3fa3 to bc7834c Compare November 16, 2026 10:03
sentrivana added a commit that referenced this pull request Nov 16, 2026
### Description

Turns on ruff's `T20`, which flags `print()` and `pprint()` calls. The
SDK runs inside users' apps, so a stray debug `print()` ends up in their
output; we should log through the SDK logger instead. `sentry_sdk` has
none today, so this is config only. Tests and scripts are excluded via
`per-file-ignores` because they print on purpose.

Each ruff-rule PR (#8282, #8283, #8285, #8286, #8287) targets `master`
on its own. They all add a line to the same `select` list in
`pyproject.toml`, so whichever merges first leaves the others with a
small conflict there to resolve.

<!-- junior-request-attribution:start -->
via @sentrivana.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:slack%3AC0CAA8NNBE3%3A1794392136.309249 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/slack%3AC0CAA8NNBE3%3A1794392136.309249)
[[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AC0CAA8NNBE3%3A1794392136.309249/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: Ivana Kellyer <131587164+sentrivana@users.noreply.github.com>
@sentrivana
sentrivana marked this pull request as ready for review November 16, 2026 10:21
@sentrivana
sentrivana requested a review from a team as a code owner November 16, 2026 10:21
@sentry-junior
sentry-junior Bot requested a review from sentrivana November 16, 2026 10:21
sentrivana added a commit that referenced this pull request Nov 16, 2026
### Description

Turns on ruff's `PT011`, which flags `pytest.raises()` with a broad
exception type such as `ValueError` or `Exception` and no `match=`.
Without `match=`, a test can pass on the wrong error.

The 27 existing offenders are fixed rather than ignored. Most get a
`match=` with the expected message. Two get a narrower exception type
(`CannotSendRequest` in the httplib test, `ZeroDivisionError` in a
skipped celery test), and `test_get_error_message` no longer misuses
`pytest.raises()` as an attribute holder.

Each ruff-rule PR (#8282, #8283, #8285, #8286, #8287) targets `master`
on its own. They all add a line to the same `select` list in
`pyproject.toml`, so whichever merges first leaves the others with a
small conflict there to resolve.

<!-- junior-request-attribution:start -->
via @sentrivana.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:slack%3AC0CAA8NNBE3%3A1794392136.309249 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/slack%3AC0CAA8NNBE3%3A1794392136.309249)
[[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AC0CAA8NNBE3%3A1794392136.309249/?project=4510944073809921)

<!-- junior-session-footer:end -->

---------

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: Ivana Kellyer <131587164+sentrivana@users.noreply.github.com>
Co-authored-by: Ivana Kellyer <ivana.kellyer@sentry.io>
@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Results 📊

✅ 139266 passed | ⏭️ 7853 skipped | Total: 147119 | Pass Rate: 94.66% | Execution Time: 459m 48s

📊 Comparison with Base Branch

Metric Change
Total Tests —
Passed Tests —
Failed Tests —
Skipped Tests —

✨ Test counts unchanged from base.

All tests are passing successfully.

✅ Patch coverage is 95.10% (97 of 102 changed executable lines covered; target 80%).
Project statement coverage is 90.21% (unchanged from base (4c1a136) to head (a12d33c)).

Changed files with executable lines (41)
File Patch coverage Changed executable lines
sentry_sdk/integrations/django/init.py 94.12% 16/17 covered; missed: 844
sentry_sdk/integrations/gcp.py 0.00% 0/1 covered; missed: 203
sentry_sdk/integrations/openai_agents/patches/error_tracing.py 50.00% 1/2 covered; missed: 36
sentry_sdk/integrations/spark/spark_driver.py 80.00% 4/5 covered; missed: 95
sentry_sdk/spotlight.py 0.00% 0/1 covered; missed: 121
sentry_sdk/_compat.py 100.00% 3/3 covered
sentry_sdk/client.py 100.00% 1/1 covered
sentry_sdk/consts.py 100.00% 1/1 covered
sentry_sdk/integrations/init.py 100.00% 1/1 covered
sentry_sdk/integrations/anthropic.py 100.00% 1/1 covered
sentry_sdk/integrations/beam.py 100.00% 3/3 covered
sentry_sdk/integrations/celery/init.py 100.00% 4/4 covered
sentry_sdk/integrations/celery/beat.py 100.00% 1/1 covered
sentry_sdk/integrations/chalice.py 100.00% 1/1 covered
sentry_sdk/integrations/django/asgi.py 100.00% 5/5 covered
sentry_sdk/integrations/django/caching.py 100.00% 4/4 covered
sentry_sdk/integrations/django/middleware.py 100.00% 2/2 covered
sentry_sdk/integrations/django/signals_handlers.py 100.00% 1/1 covered
sentry_sdk/integrations/django/tasks.py 100.00% 1/1 covered
sentry_sdk/integrations/django/templates.py 100.00% 3/3 covered
sentry_sdk/integrations/django/views.py 100.00% 4/4 covered
sentry_sdk/integrations/flask.py 100.00% 1/1 covered
sentry_sdk/integrations/openai.py 100.00% 1/1 covered
sentry_sdk/integrations/pydantic_ai/init.py 100.00% 1/1 covered
sentry_sdk/integrations/pydantic_ai/patches/tools.py 100.00% 2/2 covered
sentry_sdk/integrations/pydantic_ai/utils.py 100.00% 1/1 covered
sentry_sdk/integrations/pyramid.py 100.00% 1/1 covered
sentry_sdk/integrations/redis/init.py 100.00% 1/1 covered
sentry_sdk/integrations/redis/_async_common.py 100.00% 2/2 covered
sentry_sdk/integrations/redis/_sync_common.py 100.00% 2/2 covered
sentry_sdk/integrations/redis/rb.py 100.00% 1/1 covered
sentry_sdk/integrations/redis/redis.py 100.00% 2/2 covered
sentry_sdk/integrations/redis/redis_cluster.py 100.00% 3/3 covered
sentry_sdk/integrations/redis/redis_py_cluster_legacy.py 100.00% 1/1 covered
sentry_sdk/integrations/spark/spark_worker.py 100.00% 3/3 covered
sentry_sdk/integrations/starlette.py 100.00% 2/2 covered
sentry_sdk/integrations/threading.py 100.00% 2/2 covered
sentry_sdk/scope.py 100.00% 1/1 covered
sentry_sdk/traces.py 100.00% 1/1 covered
sentry_sdk/transport.py 100.00% 2/2 covered
sentry_sdk/utils.py 100.00% 10/10 covered
Coverage diff
@@            Coverage Diff             @@
##        master     #8285       +/-##
==========================================
  Coverage    90.21%    90.21%        —%
==========================================
  Files          203       203         —
  Tracked lines     27335     27327        -8
  Branches     10246     10246         —
==========================================
- Hits         24660     24652        -8
  Misses        2675      2675         —
  Partials      1559      1559         —

Generated by Coverage Action

sentrivana added a commit that referenced this pull request Nov 16, 2026
### Description

Turns on ruff's `RUF100`, which flags `noqa` comments that don't
suppress anything, so stale suppressions can't pile up. It also removes
the 189 that exist today, most of them `N8xx`, `E231` and `B0xx` codes
left over from the flake8 plugins, which ruff doesn't enable here. The
leftover `B`/`N` ignores and notes in `pyproject.toml` go too. Only
comments and config change; some lines got joined by `ruff format` once
their comments were gone.

Each ruff-rule PR (#8282, #8283, #8285, #8286, #8287) targets `master`
on its own. They all add a line to the same `select` list in
`pyproject.toml`, so whichever merges first leaves the others with a
small conflict there to resolve.

<!-- junior-request-attribution:start -->
via @sentrivana.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:slack%3AC0CAA8NNBE3%3A1794392136.309249 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/slack%3AC0CAA8NNBE3%3A1794392136.309249)
[[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AC0CAA8NNBE3%3A1794392136.309249/?project=4510944073809921)

<!-- junior-session-footer:end -->

---------

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: Ivana Kellyer <131587164+sentrivana@users.noreply.github.com>
@sentrivana

Copy link
Copy Markdown
Collaborator

@sentry-junior resolve the conflicts

# Conflicts:
#	pyproject.toml
#	sentry_sdk/integrations/spark/spark_driver.py
#	sentry_sdk/integrations/threading.py
@sentrivana
sentrivana merged commit 03839c9 into master Nov 16, 2026
328 of 330 checks passed
@sentrivana
sentrivana deleted the ci/ruff-inline-imports branch November 16, 2026 13:28
@linear-code

linear-code Bot commented Nov 17, 2026

Copy link
Copy Markdown

PY-3083

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants