Skip to content

TST: enforce that assert_produces_warning checks the warning message - #67275

Merged
jbrockmendel merged 1 commit into
pandas-dev:mainfrom
jbrockmendel:tst-58290-lint
Aug 29, 2026
Merged

TST: enforce that assert_produces_warning checks the warning message#67275
jbrockmendel merged 1 commit into
pandas-dev:mainfrom
jbrockmendel:tst-58290-lint

Conversation

@jbrockmendel

@jbrockmendel jbrockmendel commented Aug 29, 2026

Copy link
Copy Markdown
Member

The enforcement half of GH-58290. The four content PRs it depended on — GH-67270, GH-67271, GH-67273, GH-67274 — have all merged, so this now runs clean; rebased onto their tips.

Adds a bare_assert_produces_warning check to scripts/validate_unwanted_patterns.py, wired up as a pre-commit hook scoped to pandas/tests/. It flags tm.assert_produces_warning(SomeWarning) with no match argument.

It also covers tm.maybe_produces_warning, which forwards **kwargs straight into assert_produces_warning. Without that, swapping one helper for the other is a one-word way around the check.

Exemptions. Calls asserting that nothing is raised — assert_produces_warning(None) and (False) — are exempt, since there is no message to match. Beyond that an explicit match=None opts out, which is roughly the tm.external_warning_produced escape hatch suggested in the issue; # pdlint: ignore[bare_warning_match] also works, following the existing pdlint convention in this script.

The only place needing an exemption today is the helper's own test file, and in each case the message assertion is unreachable by construction rather than merely inconvenient:

  • TestFalseOrNoneExpectedWarning parametrizes false_or_none over [False, None], so no warning is expected at all — but the value is a name, not a literal, so a static check cannot see that.
  • test_fail_to_catch_actual_warning and test_raises_during_exception never emit the expected class, so assert_produces_warning raises Did not see expected warning of class ... before it ever looks at match — which is exactly what those tests assert.

The other four calls in that file did have an assertable message and now check it.

Verified against the full tree. pre-commit run --all-files passes on the rebased branch with no files modified, so the check reports zero violations across every file in pandas/tests/ and lands with no exclusion list. (Cross-checked that it is not silently passing by feeding it a deliberately bare call, which it flags.)

scripts/tests/test_validate_unwanted_patterns.py covers both helper names, the tm.-qualified and bare forms, each exempt spelling, the implicit Warning default, and the pdlint comment.

Closes GH#58290.

Adds a `bare_assert_produces_warning` check to
scripts/validate_unwanted_patterns.py, wired up as a pre-commit hook over
pandas/tests. It flags `tm.assert_produces_warning(SomeWarning)` (and the
`maybe_produces_warning` wrapper, which forwards **kwargs into it) when no
`match` argument is given.

Calls that assert no warning is raised -- `assert_produces_warning(None)`
and `(False)` -- are exempt, and an explicit `match=None` opts out where
there is genuinely no message to assert. That is what the helper's own
tests use: several either expect a falsy warning class or never emit the
expected class at all, so the message assertion is unreachable by
construction.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@jbrockmendel
jbrockmendel marked this pull request as ready for review August 29, 2026 22:03
@jbrockmendel
jbrockmendel merged commit 275fab9 into pandas-dev:main Aug 29, 2026
51 of 52 checks passed
@jbrockmendel
jbrockmendel deleted the tst-58290-lint branch August 29, 2026 22:52
Comment thread .pre-commit-config.yaml
Comment on lines +266 to +271
- id: unwanted-patterns-bare-assert-produces-warning
name: Check that assert_produces_warning also checks the warning message
language: python
entry: python scripts/validate_unwanted_patterns.py --validation-type="bare_assert_produces_warning"
types: [python]
files: ^pandas/tests/

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, could we change the signature of assert_produces_warning make expected_warning and match required arguments?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, will make a follow-up.

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.

TST: Disallow bare tm.assert_produces_warning

2 participants