Skip to content

Commit 76a6b7e

Browse files
authored
[pyflakes] Fix allowed-unused-imports matching for top-level modules (F401) (#20115)
<!-- Thank you for contributing to Ruff/ty! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? (Please prefix with `[ty]` for ty pull requests.) - Does this pull request include references to any relevant issues? --> ## Summary <!-- What's the purpose of the change? What does it do, and why? --> Fixes #19664 Fix allowed unused imports matching for top-level modules. I've simply replaced `from_dotted_name` with `user_defined`. Since QualifiedName for imports is created in crates/ruff_python_semantic/src/imports.rs, I guess it's acceptable to use `user_defined` here. Please tell me if there is better way. https://github.com/astral-sh/ruff/blob/0c5089ed9e180da7bc76733ffe1a1d132e9142b0/crates/ruff_python_semantic/src/imports.rs#L62 ## Test Plan <!-- How was it tested? --> I've added a snapshot test `f401_allowed_unused_imports_top_level_module`.
1 parent 1ce6571 commit 76a6b7e

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Test: allowed-unused-imports-top-level-module
3+
"""
4+
5+
# No errors
6+
7+
def f():
8+
import hvplot
9+
def f():
10+
import hvplot.pandas
11+
def f():
12+
import hvplot.pandas.plots
13+
def f():
14+
from hvplot.pandas import scatter_matrix
15+
def f():
16+
from hvplot.pandas.plots import scatter_matrix

crates/ruff_linter/src/rules/pyflakes/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,22 @@ mod tests {
376376
Ok(())
377377
}
378378

379+
#[test_case(Rule::UnusedImport, Path::new("F401_35.py"))]
380+
fn f401_allowed_unused_imports_top_level_module(rule_code: Rule, path: &Path) -> Result<()> {
381+
let diagnostics = test_path(
382+
Path::new("pyflakes").join(path).as_path(),
383+
&LinterSettings {
384+
pyflakes: pyflakes::settings::Settings {
385+
allowed_unused_imports: vec!["hvplot".to_string()],
386+
..pyflakes::settings::Settings::default()
387+
},
388+
..LinterSettings::for_rule(rule_code)
389+
},
390+
)?;
391+
assert_diagnostics!(diagnostics);
392+
Ok(())
393+
}
394+
379395
#[test]
380396
fn f841_dummy_variable_rgx() -> Result<()> {
381397
let diagnostics = test_path(

crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ pub(crate) fn unused_import(checker: &Checker, scope: &Scope) {
334334
.allowed_unused_imports
335335
.iter()
336336
.any(|allowed_unused_import| {
337-
let allowed_unused_import = QualifiedName::from_dotted_name(allowed_unused_import);
337+
let allowed_unused_import = QualifiedName::user_defined(allowed_unused_import);
338338
import.qualified_name().starts_with(&allowed_unused_import)
339339
})
340340
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
3+
---
4+

0 commit comments

Comments
 (0)