Skip to content

[ruff] Avoid false positive on ClassVar reassignment (RUF012)#21478

Merged
ntBre merged 2 commits intoastral-sh:mainfrom
Ruchir28:main
Nov 17, 2025
Merged

[ruff] Avoid false positive on ClassVar reassignment (RUF012)#21478
ntBre merged 2 commits intoastral-sh:mainfrom
Ruchir28:main

Conversation

@Ruchir28
Copy link
Contributor

Summary

Fixes #21389

Avoid RUF012 false positives when reassigning a ClassVar

Test Plan

Added the new reassignment scenario to crates/ruff_linter/resources/test/fixtures/ruff/RUF012.py.

@MichaReiser MichaReiser changed the title [lint] Enhance RUF012 rule to handle ClassVar reassignment RUF012 handle ClassVar reassignment Nov 17, 2025
@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 17, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

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 was initially picturing an approach where we looked up the assignment target in the enclosing scope to see if it had previously been the target of an annotated assignment, but I don't think that works because this lint rule runs on deferred scopes. So the approach here makes sense to me instead.

I pushed a commit fixing a couple of small nits:

  • Avoid converting the name.id to a String
  • Combine the new if-let with the existing AnnAssign match arm
  • Combine the new class_var_targets.contains check with the old iter().all() pattern

@ntBre ntBre added the rule Implementing or modifying a lint rule label Nov 17, 2025
@ntBre ntBre changed the title RUF012 handle ClassVar reassignment [ruff] Avoid false positive on ClassVar reassignment (RUF012) Nov 17, 2025
@ntBre ntBre merged commit b1e354b into astral-sh:main Nov 17, 2025
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RUF012 warns on variable re-assignment within top level of class body

2 participants