Skip to content

[ruff] Avoid false positive when pytest.raises is used in a with statement (RUF061) - #28186

Merged
ntBre merged 4 commits into
astral-sh:mainfrom
DebadityaHait:fix-28146-ruf061-with-context-false-positive
Oct 2, 2026
Merged

ntBre merged 4 commits into
astral-sh:mainfrom
DebadityaHait:fix-28146-ruf061-with-context-false-positive

Conversation

@DebadityaHait

Copy link
Copy Markdown
Contributor

Summary

Closes #28146.

Avoids false-positive RUF061 violations when pytest.raises(...), pytest.warns(...), or pytest.deprecated_call(...) is already used as the context expression in a with (or async with) statement.

Previously, passing additional positional arguments (such as with pytest.raises(ValueError, "oops"):) caused find_argument("func", 1) to match the second positional argument as the deprecated callable parameter, triggering RUF061 on code that was already using context-manager syntax.

Test Plan

  • Updated RUF061_raises.py, RUF061_warns.py, and RUF061_deprecated_call.py with test cases covering extra positional arguments in with statements and nested calls within with bodies.
  • Updated snapshots via INSTA_UPDATE=always cargo test -p ruff_linter.
  • Verified cargo fmt --check and cargo clippy -p ruff_linter --all-targets.

@astral-sh-bot
astral-sh-bot Bot requested a review from ntBre August 31, 2026 00:56
@astral-sh-bot

astral-sh-bot Bot commented Aug 31, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre ntBre changed the title [ uff] Avoid false positive when pytest.raises is used in a with statement (RUF061) [ruff] Avoid false positive when pytest.raises is used in a with statement (RUF061) Aug 31, 2026
@ntBre ntBre added the bug An issue describing something that isn't working, or a PR that fixes a bug label Aug 31, 2026

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks! I just trimmed down the tests a bit and moved to our new mdtest framework, but the code change looks good to me.

@ntBre
ntBre enabled auto-merge (squash) October 2, 2026 20:40
@ntBre
ntBre merged commit cbc71c2 into astral-sh:main Oct 2, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug An issue describing something that isn't working, or a PR that fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RUF061: false positive when arguments are mis-formed

2 participants