Skip to content

A001 and A003 should ignore variables like __doc__ #16373

@dscorbett

Description

@dscorbett

Summary

builtin-variable-shadowing (A001) and builtin-attribute-shadowing (A003) should not report violations for variables that match the names of module attributes.

For example, even though builtins.__doc__ exists, setting __doc__ in a module does not shadow it, because every module already has its own __doc__ whether you set it explicitly or not.

$ cat >a001.py <<'# EOF'
from importlib.machinery import SourceFileLoader
__doc__ = "..."
__name__ = "a001"
__loader__ = SourceFileLoader(__file__, __name__)
__package__ = None
__spec__ = None
# EOF

$ ruff --isolated check --select A001 a001.py --output-format concise
a001.py:2:1: A001 Variable `__doc__` is shadowing a Python builtin
a001.py:3:1: A001 Variable `__name__` is shadowing a Python builtin
a001.py:4:1: A001 Variable `__loader__` is shadowing a Python builtin
a001.py:5:1: A001 Variable `__package__` is shadowing a Python builtin
a001.py:6:1: A001 Variable `__spec__` is shadowing a Python builtin
Found 5 errors.

Getting a false positive for A003 is more contrived but still possible. In the following example, C.__doc__ is not shadowing builtins.__doc__. (It does shadow a003.__doc__ but that is okay because classes are allowed to have docstrings.)

$ cat >a003.py <<'# EOF'
class C:
    __doc__ = "None"
    def f() -> __doc__: ...
# EOF

$ ruff --isolated check --select A003 a003.py --output-format concise
a003.py:3:16: A003 Python builtin is shadowed by class attribute `__doc__` from line 2
Found 1 error.

Version

ruff 0.9.7 (54fccb3 2025-02-20)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions