-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strange autofix for PYI025 if "Set"
is included in __all__
#10508
Comments
What's the correct fix in your view or should the rule not emit a fix if it's a module scoped variable named |
Including the name If we stipulate that it's correct for us to emit the violation here at all, the correct fix would be to modify the original snippet to from collections.abc import Set as AbstractSet
__all__ = ["AbstractSet"] rather than what we currently do, which is to modify the snippet to this: from collections.abc import Set as AbstractSet
AbstractSet = ["Set"] This is an edge case that's unlikely to come up. However, I'm somewhat concerned that it points to a broader issue with the autofix machinery that could also impact other rules. Should we be emitting PYI025 on this snippet at all?This is debatable. However, I think it's fine to emit PYI025 even if the |
Note that the code will actually trigger F811 after being "autofixed" for PYI025: from collections.abc import Set as AbstractSet
AbstractSet = ["Set"] # F811: Redefinition of unused `AbstractSet` from line 1 |
The autofix is done using the renamer here: ruff/crates/ruff_linter/src/rules/flake8_pyi/rules/unaliased_collections_abc_set_import.rs Line 80 in 4f06d59
The issue in the renamer is here where, after renaming the binding itself ( ruff/crates/ruff_linter/src/renamer.rs Lines 202 to 206 in 4f06d59
The renamer correctly recognises that |
Adding this debug print: diff --git a/crates/ruff_linter/src/renamer.rs b/crates/ruff_linter/src/renamer.rs
index 8571d7f53..e8f4d1295 100644
--- a/crates/ruff_linter/src/renamer.rs
+++ b/crates/ruff_linter/src/renamer.rs
@@ -202,6 +202,7 @@ impl Renamer {
// Rename the references to the binding.
edits.extend(binding.references().map(|reference_id| {
let reference = semantic.reference(reference_id);
+ dbg!(reference);
Edit::range_replacement(target.to_string(), reference.range())
})); reveals that the range of the This TODO comment looks relevant @charliermarsh ;) ruff/crates/ruff_linter/src/checkers/ast/mod.rs Lines 2114 to 2120 in 4f06d59
|
The following snippet correctly causes Ruff to emit PYI025:
But if you apply the autofix, it "fixes" it to this, which is... not correct:
The text was updated successfully, but these errors were encountered: