astral-sh / astral-sh/ruff

A001 misses some attributes referenced within the class scope

Open
#20,179 5 comments 0 reactions 0 assignees View on GitHub
bug rule
Dominant language
Rust
Stars
49.6k
Forks
2.4k
Avg merge
2d 1h
Merged PRs (30d)
458

Description

### Summary

[`builtin-variable-shadowing` (A001)](https://docs.astral.sh/ruff/rules/builtin-variable-shadowing/) ignores some bindings in class scope which it checks in other scopes. [Example](https://play.ruff.rs/1ab5858c-b390-4f6a-9f89-0b98ef1cf5d9):
```console
$ cat >a001.py <<'# EOF'
class C:
for id in [1]: pass
def f1(self, x=id): return x

(id := 2)
def f2(self, x=id): return x

from contextlib import nullcontext
with nullcontext() as id: pass
def f3(self, x=id): return x

c = C()
print(c.f1(), c.f2(), c.f3())
# EOF

$ python a001.py
1 2 None

$ ruff --isolated check a001.py --select A001
All checks passed!

$ flake8 --select A a001.py
a001.py:2:5: A001 variable "id" is shadowing a Python builtin
a001.py:5:6: A001 variable "id" is shadowing a Python builtin
a001.py:9:5: A001 variable "id" is shadowing a Python builtin
```

It might make more sense to report these for [`builtin-attribute-shadowing` (A003)](https://docs.astral.sh/ruff/rules/builtin-attribute-shadowing/), but A001 is how the upstream plugin categorizes them. Either way, they are false negatives.

### Version

ruff 0.12.11 (c2bc15bc1 2025-08-28)

Contributor guide

Open the contributing guide

Research direction

Use the a001.py reproducer and run Ruff with --isolated check --select A001, comparing its output with flake8. Trace the A001 and A003 rule entry points to determine which class-scope bindings are covered, then add regression coverage so the reported cases are no longer false negatives.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.