From e914bc300b46840b0d48c5ece1c0e63418820ce0 Mon Sep 17 00:00:00 2001 From: plredmond <51248199+plredmond@users.noreply.github.com> Date: Fri, 31 May 2024 13:29:08 -0700 Subject: [PATCH] F401 sort bindings before adding to __all__ (#11648) Sort the binding IDs before passing them to the add-to-`__all__` function to address #11619. --- .../ruff_linter/src/rules/pyflakes/rules/unused_import.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 eff33fcb1cc5a..60347b06d95b6 100644 --- a/crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs +++ b/crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs @@ -330,7 +330,7 @@ pub(crate) fn unused_import(checker: &Checker, scope: &Scope, diagnostics: &mut fix_by_reexporting( checker, import_statement, - &to_reexport.iter().map(|(b, _)| b).collect::>(), + to_reexport.iter().map(|(b, _)| b).collect::>(), &dunder_all_exprs, ) .ok(), @@ -450,7 +450,7 @@ fn fix_by_removing_imports<'a>( fn fix_by_reexporting( checker: &Checker, node_id: NodeId, - imports: &[&ImportBinding], + mut imports: Vec<&ImportBinding>, dunder_all_exprs: &[&ast::Expr], ) -> Result { let statement = checker.semantic().statement(node_id); @@ -458,6 +458,8 @@ fn fix_by_reexporting( bail!("Expected import bindings"); } + imports.sort_by_key(|b| b.name); + let edits = match dunder_all_exprs { [] => fix::edits::make_redundant_alias( imports.iter().map(|b| b.import.member_name()),