ruff/crates/ruff_python_semantic/src
Charlie Marsh b9754bd5c5
Add autofix for Set-to-AbstractSet rewrite using reference tracking (#5074)
## Summary

This PR enables autofix behavior for the `flake8-pyi` rule that asks you
to alias `Set` to `AbstractSet` when importing `collections.abc.Set`.
It's not the most important rule, but it's a good isolated test-case for
local symbol renaming.

The renaming algorithm is outlined in-detail in the `renamer.rs` module.
But to demonstrate the behavior, here's the diff when running this fix
over a complex file that exercises a few edge cases:

```diff
--- a/foo.pyi
+++ b/foo.pyi
@@ -1,16 +1,16 @@
 if True:
-    from collections.abc import Set
+    from collections.abc import Set as AbstractSet
 else:
-    Set = 1
+    AbstractSet = 1

-x: Set = set()
+x: AbstractSet = set()

-x: Set
+x: AbstractSet

-del Set
+del AbstractSet

 def f():
-    print(Set)
+    print(AbstractSet)

     def Set():
         pass
```

Making this work required resolving a bunch of edge cases in the
semantic model that were causing us to "lose track" of references. For
example, the above wasn't possible with our previous approach to
handling deletions (#5071). Similarly, the `x: Set` "delayed annotation"
tracking was enabled via #5070. And many of these edits would've failed
if we hadn't changed `BindingKind` to always match the identifier range
(#5090). So it's really the culmination of a bunch of changes over the
course of the week.

The main outstanding TODO is that this doesn't support `global` or
`nonlocal` usages. I'm going to take a look at that tonight, but I'm
comfortable merging this as-is.

Closes #1106.

Closes #5091.
2023-06-16 14:12:33 +00:00
..
analyze Use const-singleton helpers in more rules (#5142) 2023-06-16 04:28:35 +00:00
binding.rs Add autofix for Set-to-AbstractSet rewrite using reference tracking (#5074) 2023-06-16 14:12:33 +00:00
context.rs Remove separate ReferenceContext enum (#4631) 2023-05-24 15:12:38 +00:00
definition.rs Run rustfmt on nightly to clean up erroneous comments (#5106) 2023-06-15 00:19:05 +00:00
globals.rs Make some of ruff_python_semantic pub(crate) (#5093) 2023-06-14 17:49:37 +00:00
lib.rs Re-export ruff_python_semantic members (#5094) 2023-06-14 18:23:38 +00:00
model.rs Track "delayed" annotations in the semantic model (#5070) 2023-06-14 17:54:35 +00:00
node.rs Make some of ruff_python_semantic pub(crate) (#5093) 2023-06-14 17:49:37 +00:00
reference.rs Make some of ruff_python_semantic pub(crate) (#5093) 2023-06-14 17:49:37 +00:00
scope.rs Run rustfmt on nightly to clean up erroneous comments (#5106) 2023-06-15 00:19:05 +00:00