-
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
[pyflakes] Fix check of unused imports #13965
Open
gpilikin
wants to merge
6
commits into
astral-sh:main
Choose a base branch
from
gpilikin:fix/unused_imports
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2698972
[pyflakes] Fix check of unused imports
gpilikin 60281d3
[pyflakes] Ignore submodule_imports in F823
gpilikin 9c27ead
fixup! [pyflakes] Ignore submodule_imports in F823
gpilikin 4f20c21
[pyflakes] Fix incorrect resolving of submodules with aliases
gpilikin 1c443cb
fixup! [pyflakes] Fix check of unused imports
gpilikin 8d0d50c
fixup! fixup! [pyflakes] Fix check of unused imports
gpilikin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,8 +1,9 @@ | ||||||||||||||||||||||||||
use std::borrow::Cow; | ||||||||||||||||||||||||||
use std::iter; | ||||||||||||||||||||||||||
use unicode_normalization::UnicodeNormalization; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
use anyhow::{anyhow, bail, Result}; | ||||||||||||||||||||||||||
use std::collections::BTreeMap; | ||||||||||||||||||||||||||
use std::collections::{BTreeMap, HashSet}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
use ruff_diagnostics::{Applicability, Diagnostic, Fix, FixAvailability, Violation}; | ||||||||||||||||||||||||||
use ruff_macros::{derive_message_formats, ViolationMetadata}; | ||||||||||||||||||||||||||
|
@@ -276,75 +277,95 @@ pub(crate) fn unused_import(checker: &Checker, scope: &Scope, diagnostics: &mut | |||||||||||||||||||||||||
// Collect all unused imports by statement. | ||||||||||||||||||||||||||
let mut unused: BTreeMap<(NodeId, Exceptions), Vec<ImportBinding>> = BTreeMap::default(); | ||||||||||||||||||||||||||
let mut ignored: BTreeMap<(NodeId, Exceptions), Vec<ImportBinding>> = BTreeMap::default(); | ||||||||||||||||||||||||||
let mut already_checked_imports: HashSet<String> = HashSet::new(); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
for binding_id in scope.binding_ids() { | ||||||||||||||||||||||||||
let binding = checker.semantic().binding(binding_id); | ||||||||||||||||||||||||||
let top_binding = checker.semantic().binding(binding_id); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if binding.is_used() | ||||||||||||||||||||||||||
|| binding.is_explicit_export() | ||||||||||||||||||||||||||
|| binding.is_nonlocal() | ||||||||||||||||||||||||||
|| binding.is_global() | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
let Some(import) = binding.as_any_import() else { | ||||||||||||||||||||||||||
let Some(_) = top_binding.as_any_import() else { | ||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
let Some(node_id) = binding.source else { | ||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
let name = binding.name(checker.source()); | ||||||||||||||||||||||||||
let binding_name = top_binding | ||||||||||||||||||||||||||
.name(checker.source()) | ||||||||||||||||||||||||||
.split('.') | ||||||||||||||||||||||||||
.next() | ||||||||||||||||||||||||||
.unwrap_or(""); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// If an import is marked as required, avoid treating it as unused, regardless of whether | ||||||||||||||||||||||||||
// it was _actually_ used. | ||||||||||||||||||||||||||
if checker | ||||||||||||||||||||||||||
.settings | ||||||||||||||||||||||||||
.isort | ||||||||||||||||||||||||||
.required_imports | ||||||||||||||||||||||||||
.iter() | ||||||||||||||||||||||||||
.any(|required_import| required_import.matches(name, &import)) | ||||||||||||||||||||||||||
for binding in scope | ||||||||||||||||||||||||||
.get_all(&binding_name.nfkc().collect::<String>()) | ||||||||||||||||||||||||||
.map(|binding_id| checker.semantic().binding(binding_id)) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
let name = binding.name(checker.source()); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// If an import was marked as allowed, avoid treating it as unused. | ||||||||||||||||||||||||||
if checker | ||||||||||||||||||||||||||
.settings | ||||||||||||||||||||||||||
.pyflakes | ||||||||||||||||||||||||||
.allowed_unused_imports | ||||||||||||||||||||||||||
.iter() | ||||||||||||||||||||||||||
.any(|allowed_unused_import| { | ||||||||||||||||||||||||||
let allowed_unused_import = QualifiedName::from_dotted_name(allowed_unused_import); | ||||||||||||||||||||||||||
import.qualified_name().starts_with(&allowed_unused_import) | ||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
if already_checked_imports.contains(name) { | ||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
already_checked_imports.insert(name.to_string()); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
let import = ImportBinding { | ||||||||||||||||||||||||||
name, | ||||||||||||||||||||||||||
import, | ||||||||||||||||||||||||||
range: binding.range(), | ||||||||||||||||||||||||||
parent_range: binding.parent_range(checker.semantic()), | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
if binding.is_used() | ||||||||||||||||||||||||||
|| binding.is_explicit_export() | ||||||||||||||||||||||||||
|| binding.is_nonlocal() | ||||||||||||||||||||||||||
|| binding.is_global() | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if checker.rule_is_ignored(Rule::UnusedImport, import.start()) | ||||||||||||||||||||||||||
|| import.parent_range.is_some_and(|parent_range| { | ||||||||||||||||||||||||||
checker.rule_is_ignored(Rule::UnusedImport, parent_range.start()) | ||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
ignored | ||||||||||||||||||||||||||
.entry((node_id, binding.exceptions)) | ||||||||||||||||||||||||||
.or_default() | ||||||||||||||||||||||||||
.push(import); | ||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||
unused | ||||||||||||||||||||||||||
.entry((node_id, binding.exceptions)) | ||||||||||||||||||||||||||
.or_default() | ||||||||||||||||||||||||||
.push(import); | ||||||||||||||||||||||||||
let Some(import) = binding.as_any_import() else { | ||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
let Some(stmt_id) = binding.source else { | ||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// If an import is marked as required, avoid treating it as unused, regardless of whether | ||||||||||||||||||||||||||
// it was _actually_ used. | ||||||||||||||||||||||||||
if checker | ||||||||||||||||||||||||||
.settings | ||||||||||||||||||||||||||
.isort | ||||||||||||||||||||||||||
.required_imports | ||||||||||||||||||||||||||
.iter() | ||||||||||||||||||||||||||
.any(|required_import| required_import.matches(name, &import)) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
// If an import was marked as allowed, avoid treating it as unused. | ||||||||||||||||||||||||||
if checker.settings.pyflakes.allowed_unused_imports.iter().any( | ||||||||||||||||||||||||||
|allowed_unused_import| { | ||||||||||||||||||||||||||
let allowed_unused_import = | ||||||||||||||||||||||||||
QualifiedName::from_dotted_name(allowed_unused_import); | ||||||||||||||||||||||||||
import.qualified_name().starts_with(&allowed_unused_import) | ||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
let import = ImportBinding { | ||||||||||||||||||||||||||
name, | ||||||||||||||||||||||||||
import, | ||||||||||||||||||||||||||
range: binding.range(), | ||||||||||||||||||||||||||
parent_range: binding.parent_range(checker.semantic()), | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if checker.rule_is_ignored(Rule::UnusedImport, import.start()) | ||||||||||||||||||||||||||
|| import.parent_range.is_some_and(|parent_range| { | ||||||||||||||||||||||||||
checker.rule_is_ignored(Rule::UnusedImport, parent_range.start()) | ||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||
ignored | ||||||||||||||||||||||||||
.entry((stmt_id, binding.exceptions)) | ||||||||||||||||||||||||||
.or_default() | ||||||||||||||||||||||||||
.push(import); | ||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||
unused | ||||||||||||||||||||||||||
.entry((stmt_id, binding.exceptions)) | ||||||||||||||||||||||||||
.or_default() | ||||||||||||||||||||||||||
.push(import); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, I'll take a look