ruff/crates/ruff_python_semantic/src
Charlie Marsh 25bafd2d66
Restrict builtin-attribute-shadowing to actual shadowed references (#9462)
## Summary

This PR attempts to improve `builtin-attribute-shadowing` (`A003`), a
rule which has been repeatedly criticized, but _does_ have value (just
not in the current form).

Historically, this rule would flag cases like:

```python
class Class:
    id: int
```

This led to an increasing number of exceptions and special-cases to the
rule over time to try and improve it's specificity (e.g., ignore
`TypedDict`, ignore `@override`).

The crux of the issue is that given the above, referencing `id` will
never resolve to `Class.id`, so the shadowing is actually fine. There's
one exception, however:

```python
class Class:
    id: int

    def do_thing() -> id:
        pass
```

Here, `id` actually resolves to the `id` attribute on the class, not the
`id` builtin.

So this PR completely reworks the rule around this _much_ more targeted
case, which will almost always be a mistake: when you reference a class
member from within the class, and that member shadows a builtin.

Closes https://github.com/astral-sh/ruff/issues/6524.

Closes https://github.com/astral-sh/ruff/issues/7806.
2024-01-11 12:59:40 -05:00
..
analyze Handle raises with implicit alternate branches (#9377) 2024-01-02 22:59:12 -05:00
binding.rs Add a BindingKind for WithItem variables (#8594) 2023-11-09 22:44:49 -05:00
branches.rs Add branch detection to the semantic model (#6694) 2023-08-19 21:28:17 +00:00
context.rs
definition.rs Misc. small tweaks from perusing modules (#9383) 2024-01-03 12:30:25 -05:00
globals.rs Move Ranged into ruff_text_size (#6919) 2023-08-27 14:12:51 -04:00
lib.rs Use a single node hierarchy to track statements and expressions (#6709) 2023-08-21 21:32:57 -04:00
model.rs Restrict builtin-attribute-shadowing to actual shadowed references (#9462) 2024-01-11 12:59:40 -05:00
nodes.rs Move {AnyNodeRef, AstNode} to ruff_python_ast crate root (#8030) 2023-10-18 00:01:18 +00:00
reference.rs Allow flake8-type-checking rules to automatically quote runtime-evaluated references (#6001) 2023-12-13 03:12:38 +00:00
scope.rs Remove async AST node variants for with, for, and def (#6369) 2023-08-07 16:36:02 +00:00
star_import.rs Move StarImport to its own module (#5186) 2023-06-20 13:12:46 -04:00