Skip to content

Commit

Permalink
charlie patch to remove the unfinished dunder_all mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
plredmond committed May 2, 2024
1 parent dacbe73 commit 31768b6
Showing 1 changed file with 7 additions and 25 deletions.
32 changes: 7 additions & 25 deletions crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::rules::{isort, isort::ImportSection, isort::ImportType};
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
enum UnusedImportContext {
ExceptHandler,
Init { first_party: bool, dunder_all: bool },
Init { first_party: bool },
}

/// ## What it does
Expand Down Expand Up @@ -108,14 +108,7 @@ impl Violation for UnusedImport {
fn fix_title(&self) -> Option<String> {
let UnusedImport { name, multiple, .. } = self;
let resolution = match self.context {
Some(UnusedImportContext::Init {
first_party: true,
dunder_all: true,
}) => "Add unused import to __all__",
Some(UnusedImportContext::Init {
first_party: true,
dunder_all: false,
}) => "Use a redundant alias",
Some(UnusedImportContext::Init { first_party: true }) => "Use a redundant alias",
_ => "Remove unused import",
};
Some(if *multiple {
Expand Down Expand Up @@ -147,7 +140,7 @@ fn is_first_party(qualified_name: &str, level: u32, checker: &Checker) -> bool {

/// For some unused binding in an import statement...
///
/// __init__.py ∧ 1stpty → safe, move to __all__ or convert to explicit-import
/// __init__.py ∧ 1stpty → safe, convert to redundant-alias
/// __init__.py ∧ stdlib → unsafe, remove
/// __init__.py ∧ 3rdpty → unsafe, remove
///
Expand Down Expand Up @@ -204,8 +197,6 @@ pub(crate) fn unused_import(checker: &Checker, scope: &Scope, diagnostics: &mut

let in_init = checker.path().ends_with("__init__.py");
let fix_init = checker.settings.preview.is_enabled();
// TODO: find the `__all__` node
let dunder_all = None;

// Generate a diagnostic for every import, but share fixes across all imports within the same
// statement (excluding those that are ignored).
Expand Down Expand Up @@ -234,7 +225,6 @@ pub(crate) fn unused_import(checker: &Checker, scope: &Scope, diagnostics: &mut
level,
checker,
),
dunder_all: dunder_all.is_some(),
})
} else {
None
Expand All @@ -244,10 +234,7 @@ pub(crate) fn unused_import(checker: &Checker, scope: &Scope, diagnostics: &mut
.partition(|(_, context)| {
matches!(
context,
Some(UnusedImportContext::Init {
first_party: true,
..
})
Some(UnusedImportContext::Init { first_party: true })
)
});

Expand All @@ -265,7 +252,6 @@ pub(crate) fn unused_import(checker: &Checker, scope: &Scope, diagnostics: &mut
checker,
import_statement,
to_reexport.iter().map(|(binding, _)| binding),
dunder_all,
)
.ok(),
)
Expand Down Expand Up @@ -378,13 +364,12 @@ fn fix_by_removing_imports<'a>(
)
}

/// Generate a [`Fix`] to make bindings in a statement explicit, either by adding them to `__all__`
/// or changing them from `import a` to `import a as a`.
/// Generate a [`Fix`] to make bindings in a statement explicit, by changing from `import a` to
/// `import a as a`.
fn fix_by_reexporting<'a>(
checker: &Checker,
node_id: NodeId,
imports: impl Iterator<Item = &'a ImportBinding<'a>>,
dunder_all: Option<NodeId>,
) -> Result<Fix> {
let statement = checker.semantic().statement(node_id);

Expand All @@ -395,10 +380,7 @@ fn fix_by_reexporting<'a>(
bail!("Expected import bindings");
}

let edits = match dunder_all {
Some(_dunder_all) => bail!("Not implemented: add to dunder_all"),
None => fix::edits::make_redundant_alias(member_names.iter().map(AsRef::as_ref), statement),
};
let edits = fix::edits::make_redundant_alias(member_names.iter().map(AsRef::as_ref), statement);

// Only emit a fix if there are edits
let mut tail = edits.into_iter();
Expand Down

0 comments on commit 31768b6

Please sign in to comment.