diff --git a/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_35.py b/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_35.py new file mode 100644 index 0000000000..0b1cb0644f --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_35.py @@ -0,0 +1,16 @@ +""" +Test: allowed-unused-imports-top-level-module +""" + +# No errors + +def f(): + import hvplot +def f(): + import hvplot.pandas +def f(): + import hvplot.pandas.plots +def f(): + from hvplot.pandas import scatter_matrix +def f(): + from hvplot.pandas.plots import scatter_matrix diff --git a/crates/ruff_linter/src/rules/pyflakes/mod.rs b/crates/ruff_linter/src/rules/pyflakes/mod.rs index 12b7d5ce86..ab553e2b72 100644 --- a/crates/ruff_linter/src/rules/pyflakes/mod.rs +++ b/crates/ruff_linter/src/rules/pyflakes/mod.rs @@ -376,6 +376,22 @@ mod tests { Ok(()) } + #[test_case(Rule::UnusedImport, Path::new("F401_35.py"))] + fn f401_allowed_unused_imports_top_level_module(rule_code: Rule, path: &Path) -> Result<()> { + let diagnostics = test_path( + Path::new("pyflakes").join(path).as_path(), + &LinterSettings { + pyflakes: pyflakes::settings::Settings { + allowed_unused_imports: vec!["hvplot".to_string()], + ..pyflakes::settings::Settings::default() + }, + ..LinterSettings::for_rule(rule_code) + }, + )?; + assert_diagnostics!(diagnostics); + Ok(()) + } + #[test] fn f841_dummy_variable_rgx() -> Result<()> { let diagnostics = test_path( diff --git a/crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs b/crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs index 5af5312af5..130c614ae7 100644 --- a/crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs +++ b/crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs @@ -334,7 +334,7 @@ pub(crate) fn unused_import(checker: &Checker, scope: &Scope) { .allowed_unused_imports .iter() .any(|allowed_unused_import| { - let allowed_unused_import = QualifiedName::from_dotted_name(allowed_unused_import); + let allowed_unused_import = QualifiedName::user_defined(allowed_unused_import); import.qualified_name().starts_with(&allowed_unused_import) }) { diff --git a/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__f401_allowed_unused_imports_top_level_module.snap b/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__f401_allowed_unused_imports_top_level_module.snap new file mode 100644 index 0000000000..d0b409f39e --- /dev/null +++ b/crates/ruff_linter/src/rules/pyflakes/snapshots/ruff_linter__rules__pyflakes__tests__f401_allowed_unused_imports_top_level_module.snap @@ -0,0 +1,4 @@ +--- +source: crates/ruff_linter/src/rules/pyflakes/mod.rs +--- +