From e57f900f5b75b7bcedde25bd03ca4cc69ba43e33 Mon Sep 17 00:00:00 2001 From: PLR <51248199+plredmond@users.noreply.github.com> Date: Fri, 26 Apr 2024 17:00:31 -0700 Subject: [PATCH] [ruff F401 #10390 #10391] fixture tests --- .../fixtures/pyflakes/F401_24/__init__.py | 36 +++++++++++++++++ .../test/fixtures/pyflakes/F401_24/aliased.py | 0 .../test/fixtures/pyflakes/F401_24/renamed.py | 0 .../test/fixtures/pyflakes/F401_24/unused.py | 0 .../test/fixtures/pyflakes/F401_24/used.py | 0 .../pyflakes/F401_25_dunder_all/__init__.py | 40 +++++++++++++++++++ .../pyflakes/F401_25_dunder_all/aliased.py | 0 .../pyflakes/F401_25_dunder_all/renamed.py | 0 .../pyflakes/F401_25_dunder_all/unused.py | 0 .../pyflakes/F401_25_dunder_all/used.py | 0 crates/ruff_linter/src/rules/pyflakes/mod.rs | 2 + .../src/rules/pyflakes/rules/unused_import.rs | 5 ++- 12 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 crates/ruff_linter/resources/test/fixtures/pyflakes/F401_24/__init__.py create mode 100644 crates/ruff_linter/resources/test/fixtures/pyflakes/F401_24/aliased.py create mode 100644 crates/ruff_linter/resources/test/fixtures/pyflakes/F401_24/renamed.py create mode 100644 crates/ruff_linter/resources/test/fixtures/pyflakes/F401_24/unused.py create mode 100644 crates/ruff_linter/resources/test/fixtures/pyflakes/F401_24/used.py create mode 100644 crates/ruff_linter/resources/test/fixtures/pyflakes/F401_25_dunder_all/__init__.py create mode 100644 crates/ruff_linter/resources/test/fixtures/pyflakes/F401_25_dunder_all/aliased.py create mode 100644 crates/ruff_linter/resources/test/fixtures/pyflakes/F401_25_dunder_all/renamed.py create mode 100644 crates/ruff_linter/resources/test/fixtures/pyflakes/F401_25_dunder_all/unused.py create mode 100644 crates/ruff_linter/resources/test/fixtures/pyflakes/F401_25_dunder_all/used.py diff --git a/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_24/__init__.py b/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_24/__init__.py new file mode 100644 index 00000000000000..b29e31d2cacc17 --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_24/__init__.py @@ -0,0 +1,36 @@ +'''__init__.py without __all__ + +Stdlib and third party imports are unsafe removals + +First party imports get changed to redundant aliases +''' + + + +import os # Ok: is used +_ = os + + + +import sys # F401: remove unused + + + +import used # Ok: is used +_ = used + + + + + + + +import aliased as aliased # Ok: is redundant alias + + + +import renamed as bees # Ok: is aliased + + + +import unused # F401: change to redundant alias diff --git a/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_24/aliased.py b/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_24/aliased.py new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_24/renamed.py b/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_24/renamed.py new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_24/unused.py b/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_24/unused.py new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_24/used.py b/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_24/used.py new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_25_dunder_all/__init__.py b/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_25_dunder_all/__init__.py new file mode 100644 index 00000000000000..6ea5c28976ef9c --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_25_dunder_all/__init__.py @@ -0,0 +1,40 @@ +'''__init__.py with __all__ + +First party imports get added to __all__ + +Stdlib and third party imports are unsafe removals +''' + + + +import os # Ok: is used +_ = os + + + +import sys # F401: remove unused + + + +import argparse # Ok: is exported + + + +import used # Ok: is used +_ = used + + + +import aliased as aliased # Ok: is redundant alias + + + +import renamed as bees # Ok: is aliased + + + +import unused # F401: add to __all__ + + + +__all__ = ["argparse"] diff --git a/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_25_dunder_all/aliased.py b/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_25_dunder_all/aliased.py new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_25_dunder_all/renamed.py b/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_25_dunder_all/renamed.py new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_25_dunder_all/unused.py b/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_25_dunder_all/unused.py new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_25_dunder_all/used.py b/crates/ruff_linter/resources/test/fixtures/pyflakes/F401_25_dunder_all/used.py new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/crates/ruff_linter/src/rules/pyflakes/mod.rs b/crates/ruff_linter/src/rules/pyflakes/mod.rs index 6f5673b1ecc39d..cc5442a2ec4638 100644 --- a/crates/ruff_linter/src/rules/pyflakes/mod.rs +++ b/crates/ruff_linter/src/rules/pyflakes/mod.rs @@ -56,6 +56,8 @@ mod tests { #[test_case(Rule::UnusedImport, Path::new("F401_21.py"))] #[test_case(Rule::UnusedImport, Path::new("F401_22.py"))] #[test_case(Rule::UnusedImport, Path::new("F401_23.py"))] + #[test_case(Rule::UnusedImport, Path::new("F401_24/__init__.py"))] + #[test_case(Rule::UnusedImport, Path::new("F401_25_dunder_all/__init__.py"))] #[test_case(Rule::ImportShadowedByLoopVar, Path::new("F402.py"))] #[test_case(Rule::ImportShadowedByLoopVar, Path::new("F402.ipynb"))] #[test_case(Rule::UndefinedLocalWithImportStar, Path::new("F403.py"))] 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 11636905b1697f..64832847a146bf 100644 --- a/crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs +++ b/crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs @@ -116,7 +116,7 @@ impl Violation for UnusedImport { fn is_first_party(checker: &Checker, qualified_name: &str) -> bool { use isort::{ImportSection, ImportType}; - match isort::categorize( + let category = isort::categorize( qualified_name, None, &checker.settings.src, @@ -127,7 +127,8 @@ fn is_first_party(checker: &Checker, qualified_name: &str) -> bool { checker.settings.isort.no_sections, &checker.settings.isort.section_order, &checker.settings.isort.default_section, - ) { + ); + match category { ImportSection::Known(ImportType::FirstParty | ImportType::LocalFolder) => true, _ => false, }