Skip to content

TST: require expected_warning and match in assert_produces_warning - #67513

Merged
mroeschke merged 1 commit into
pandas-dev:mainfrom
jbrockmendel:ref-assert_produces_warning
Aug 31, 2026
Merged

TST: require expected_warning and match in assert_produces_warning#67513
mroeschke merged 1 commit into
pandas-dev:mainfrom
jbrockmendel:ref-assert_produces_warning

Conversation

@jbrockmendel

Copy link
Copy Markdown
Member

Follow-up to GH-67275, taking up @mroeschke's suggestion there: instead of a pre-commit check, enforce the message assertion in the signature of assert_produces_warning itself.

  • expected_warning becomes a required positional. Dropping its Warning default removes a footgun the lint check did not address at all: assert_produces_warning(match="...") previously accepted any warning class.
  • match defaults to lib.no_default; passing a truthy expected_warning without it raises TypeError. match=None remains the opt-out, the same spelling the lint check accepted.
  • maybe_produces_warning takes match as a required keyword-only argument, so the condition=False branch is covered too.

Making match unconditionally required was the other option, but it would mean adding a meaningless match=None to the 221 assert_produces_warning(None) / (False) calls, which assert that nothing is warned and so have no message to match. The sentinel keeps those untouched.

This supersedes the bare_assert_produces_warning check, which is removed here along with its pre-commit hook and script tests. The signature covers every call site in the repo rather than just pandas/tests/, sees through variables (the static check needed a hand-written exemption for a parametrized false_or_none), and needs no maintained script. The one thing it gives up is a bare call in a test that never runs in any CI job.

An AST sweep of the whole repo found 48 calls relying on the old Warning default, all in pandas/tests/interchange/; those now name Pandas4Warning explicitly. No call anywhere passed a truthy expected_warning without matchGH-67275 had already fixed those — so there is no other call-site churn.

Full pandas/tests run is green (244207 passed, 8486 skipped, 1510 xfailed) and pre-commit run --all-files passes with no files modified.

AI disclosure: drafted with Claude Code (claude opus 5 (high)), which prototyped the signature change, ran the AST sweep over call sites, and ran the test suite and pre-commit.

Follow-up to GH#67275. Enforce the message assertion in the signature of
assert_produces_warning itself rather than with a pre-commit check:
expected_warning becomes a required positional and match must be passed
whenever a warning is expected, with match=None as the opt-out.

Drops the Warning default for expected_warning, so a call can no longer
accept any warning class by accident, and removes the now-redundant
bare_assert_produces_warning check along with its hook and tests.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Comment thread pandas/_testing/_warnings.py
@jbrockmendel
jbrockmendel marked this pull request as ready for review August 31, 2026 14:42
@mroeschke
mroeschke merged commit 3a0c69b into pandas-dev:main Aug 31, 2026
56 of 61 checks passed
@mroeschke

Copy link
Copy Markdown
Member

Thanks @jbrockmendel

@jbrockmendel
jbrockmendel deleted the ref-assert_produces_warning branch August 31, 2026 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Clean Testing pandas testing functions or related to the test suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants