Skip to content

Commit

Permalink
[ruff F401 #10390 #10391] add fixture tests for F401 w/__init__.py; r…
Browse files Browse the repository at this point in the history
…ework after discussion w/zanie
  • Loading branch information
plredmond committed Apr 29, 2024
1 parent 3687485 commit 54a7cd9
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'''__init__.py without __all__
Unused stdlib and third party imports are unsafe removals
Unused first party imports get changed to redundant aliases
'''



# stdlib



import os # Ok: is used
_ = os



import argparse as argparse # Ok: is redundant alias



import sys # F401: remove unused



# first-party



from . import used # Ok: is used
_ = used



from . import aliased as aliased # Ok: is redundant alias






from . import unused # F401: change to redundant alias



from . import renamed as bees # F401: no fix
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'''__init__.py with __all__
Unused stdlib and third party imports are unsafe removals
Unused first party imports get added to __all__
'''



# stdlib



import os # Ok: is used
_ = os



import argparse # Ok: is exported in __all__



import sys # F401: remove unused



# first-party



from . import used # Ok: is used
_ = used



from . import aliased as aliased # Ok: is redundant alias



from . import exported # Ok: is exported in __all__



#from . import unused # F401: add to __all__



#from . import renamed as bees # F401: add to __all__



__all__ = ["argparse", "exported"]
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions crates/ruff_linter/src/rules/pyflakes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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__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"))]
Expand Down
5 changes: 3 additions & 2 deletions crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
}
Expand Down

0 comments on commit 54a7cd9

Please sign in to comment.