Skip to content

ci(ruff): Enable PT011 for overly broad pytest.raises() - #8286

Merged
sentrivana merged 3 commits into
masterfrom
ci/ruff-pytest-raises-too-broad
Nov 16, 2026
Merged

sentrivana merged 3 commits into
masterfrom
ci/ruff-pytest-raises-too-broad

Conversation

@sentry-junior

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

Copy link
Copy Markdown
Contributor

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.

via @sentrivana.

--

View Session [Sentry]

@sentrivana

Copy link
Copy Markdown
Collaborator

@sentry-junior Try to fix the current offenders instead of ignoring them

@sentry-junior
sentry-junior Bot force-pushed the ci/ruff-pytest-raises-too-broad branch from 7fe277b to f798939 Compare November 16, 2026 09:52
Existing cases get a noqa comment, so only new tests are checked.

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-inline-imports to master November 16, 2026 10:03
@sentry-junior
sentry-junior Bot force-pushed the ci/ruff-pytest-raises-too-broad branch from f798939 to 1c36bb2 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>
Add match= with the expected message to every pytest.raises() that
PT011 flagged, and drop the noqa comments. Where a test already
asserted on the message afterwards, the assertion moves into match=.

- test_get_error_message used pytest.raises() only as an attribute
  holder, so it now builds a plain Exception instead.
- The httplib test expects http.client.CannotSendRequest.
- The skipped celery redis-backend test expects the task's
  ZeroDivisionError; the Celery 4.1 workaround predates the minimum
  supported Celery version (4.4.7).

Co-Authored-By: Ivana Kellyer <ivana.kellyer@sentry.io>
@sentry-junior

sentry-junior Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Done in 30362cd. All 27 noqa: PT011 comments are gone, and the offending tests are fixed:

  • 24 sites now pass match= with the message the test actually raises, for example "Fetch aborted" in wsgi, "API rate limit error" in langchain, and "Unknown profiler mode: foo". Where a test already asserted on str(exc.value) afterwards (asgi, chalice, exc_info_from_error), that assertion moved into match=.
  • test_get_error_message used pytest.raises(BaseException) only to get an object to set attributes on. It now builds a plain Exception().
  • httplib now expects http.client.CannotSendRequest instead of Exception.
  • celery test_redis_backend_trace_propagation now expects the task's ZeroDivisionError. The "Celery 4.1 raises a gibberish exception" workaround predates our minimum supported Celery version, 4.4.7. This test is permanently @pytest.mark.skipped, so nothing actually runs it.

The other changed tests pass locally on py3.14 in the common (scoped), asgi, boto3, chalice and langchain-base envs.

@sentrivana
sentrivana marked this pull request as ready for review November 16, 2026 10:20
@sentrivana
sentrivana requested a review from a team as a code owner November 16, 2026 10:20
@sentry-junior
sentry-junior Bot requested a review from sentrivana November 16, 2026 10:20
@sentrivana
sentrivana enabled auto-merge (squash) November 16, 2026 10:24
@sentrivana
sentrivana merged commit ddd4948 into master Nov 16, 2026
172 checks passed
@sentrivana
sentrivana deleted the ci/ruff-pytest-raises-too-broad branch November 16, 2026 10:27
@github-actions

Copy link
Copy Markdown
Contributor

Coverage Results 📊

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

All tests are passing successfully.

✅ Patch coverage is 100.00% (no changed executable lines found; target 80%).
Project statement coverage is 90.22% (unchanged from base (41b0294) to head (8808e91)).

Coverage diff
@@            Coverage Diff             @@
##        master     #8286       +/-##
==========================================
  Coverage    90.22%    90.22%        —%
==========================================
  Files          203       203         —
  Tracked lines     27335     27335         —
  Branches     10246     10246         —
==========================================
  Hits         24661     24661         —
  Misses        2674      2674         —
  Partials      1558      1558         —

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 added a commit that referenced this pull request Nov 16, 2026
### 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.

<!-- 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>
@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