Skip to content

[pygrep-hooks] Detect non-existent mock methods in standalone expressions (PGH005)#22830

Merged
ntBre merged 4 commits intoastral-sh:mainfrom
anishgirianish:pgh005-standalone-mock
Feb 5, 2026
Merged

[pygrep-hooks] Detect non-existent mock methods in standalone expressions (PGH005)#22830
ntBre merged 4 commits intoastral-sh:mainfrom
anishgirianish:pgh005-standalone-mock

Conversation

@anishgirianish
Copy link
Contributor

Summary

Extends PGH005 to detect non-existent mock methods like mock.not_called() when used as
standalone expressions, not just inside assert statements.

Closes #13252

Test Plan

cargo nextest run -p ruff_linter pygrep_hooks

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thank you! This looks like a nice, straightforward change. Let's make it a preview change, though, since it's a big expansion to a stable rule.

I'll also be curious to see how the ecosystem results look once CI runs. Hopefully there aren't too many false positives, as Alex mentioned on the issue.

@ntBre ntBre added rule Implementing or modifying a lint rule preview Related to preview mode features labels Feb 4, 2026
@astral-sh-bot
Copy link

astral-sh-bot bot commented Feb 4, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre
Copy link
Contributor

ntBre commented Feb 4, 2026

Well, I wouldn't worry too much about these ecosystem checks. Such spurious results are usually just from CI not being approved soon enough. Once you push another commit, I'd expect them to clear up.

@anishgirianish
Copy link
Contributor Author

@ntBre Thanks for the review! I've gated the standalone expression detection behind preview mode

  • Added is_standalone_mock_non_existent_enabled helper in preview.rs
  • Wrapped the non_existent_mock_method call in the Stmt::Expr branch behind the preview check the existing Stmt::Assert detection remains stable
  • Added a preview_rules test using assert_diagnostics_diff!

Stable behavior is unchanged; the standalone expansion only fires in preview.

Would like to request you for your re-review. thank you

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thank you! I'll wait for the ecosystem check, but assuming it looks good, I'll merge this right after.

@ntBre ntBre changed the title [pygrep-hooks] Detect non-existent mock methods in standalone expressions (PGH005 [pygrep-hooks] Detect non-existent mock methods in standalone expressions (PGH005) Feb 5, 2026
@ntBre ntBre merged commit eca0b87 into astral-sh:main Feb 5, 2026
41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

preview Related to preview mode features rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Detect not_called() and similar mistakes with Python mocks before Python 3.12

2 participants