Skip to content

[flake8-builtins] Expand checks in class scopes (A001) - #29076

Merged
ntBre merged 4 commits into
astral-sh:mainfrom
lognd:a001-class-scope-bindings
Oct 2, 2026
Merged

ntBre merged 4 commits into
astral-sh:mainfrom
lognd:a001-class-scope-bindings

Conversation

@lognd

@lognd lognd commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Inside a class body, flake8-builtins reports a for, with ... as, or := target that shadows a builtin, but A001 does not. The A001 call site skipped every stored name in class scope because A003 handles class attributes, but A003 only considers assignment and annotation bindings, so those other binding kinds were reported by neither rule. The class-scope skip now applies only to the binding kinds A003 handles, by checking the stored name's BindingKind. Attribute and method bindings in a class still go to A003, so there's no double-reporting.

This is the gap left after #20178, which I picked up because no pull request was opened for it.

Fixes #20179

Test Plan

I have added new class-scope cases in the A001 fixture covering both the three bindings that now fire and the attribute and method bindings that stay silent, updated the snapshot with three new diagnostics, and ran the flake8-builtins tests, clippy, and fmt.

lognd added 2 commits October 2, 2026 10:13
…gs (`A001`)

`builtin-variable-shadowing` skipped every stored name in a class body
on the grounds that `builtin-attribute-shadowing` (A003) covers class
attributes. A003 only considers plain assignment and annotation
bindings, so loop targets, `with ... as` targets and walrus targets in a
class body were reported by neither rule. Narrow the class-scope skip to
the binding kinds A003 actually handles.

Fixes astral-sh#20179
Cover `for`, `:=` and `with ... as` targets in a class body, plus the
attribute and method bindings that stay with A003, and refresh the
snapshot.

Refs astral-sh#20179
@astral-sh-bot
astral-sh-bot Bot requested a review from ntBre October 2, 2026 17:03

@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, this looks good to me logic-wise. Let's just move the check into the rule code.

Comment on lines +389 to +400
// In class scope, attribute bindings (e.g., `id = 1`) are covered by
// `builtin-attribute-shadowing` (A003), but other bindings (e.g., a
// `for` target or `:=`) are not and still shadow the builtin.
let scope = checker.semantic.current_scope();
let is_class_attribute = scope.kind.is_class()
&& scope.get(id).is_none_or(|binding_id| {
matches!(
checker.semantic.binding(binding_id).kind,
BindingKind::Assignment | BindingKind::Annotation
)
});
if !is_class_attribute {

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.

I initially missed that there was an existing scope check here. Now that the logic is more involved, I think it's a better fit to move it into the rule function itself.

@ntBre ntBre changed the title [flake8-builtins] Detect class-scope for, with, and := bindings (A001) [flake8-builtins] Expand checks in class scopes (A001) Oct 2, 2026
@ntBre ntBre added the bug An issue describing something that isn't working, or a PR that fixes a bug label Oct 2, 2026
@astral-sh-bot

astral-sh-bot Bot commented Oct 2, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

lognd added 2 commits October 2, 2026 14:24
…_shadowing`

Keep the decision about which class-scope bindings A003 already covers
inside the rule rather than at the call site.
@lognd

lognd commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

I moved the check into the rule code; thanks for pointing that out.

@ntBre
ntBre enabled auto-merge (squash) October 2, 2026 18:33
@lognd
lognd requested a review from ntBre October 2, 2026 18:33
@lognd

lognd commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

Rerequested review by accident, nothing new. Thanks for being speedy!

@ntBre
ntBre merged commit b4c75cd into astral-sh:main Oct 2, 2026
51 checks passed
@lognd
lognd deleted the a001-class-scope-bindings branch October 2, 2026 18:42
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.

A001 misses some attributes referenced within the class scope

2 participants