mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-16 08:30:16 +00:00
Select stable import name when multiple possible bindings are in scope (#12888)
This commit is contained in:
parent
499c0bd875
commit
d61d75d4fa
2 changed files with 27 additions and 12 deletions
|
@ -907,7 +907,7 @@ impl<'a> SemanticModel<'a> {
|
|||
self.current_scopes()
|
||||
.enumerate()
|
||||
.find_map(|(scope_index, scope)| {
|
||||
scope.bindings().find_map(|(name, binding_id)| {
|
||||
let mut imported_names = scope.bindings().filter_map(|(name, binding_id)| {
|
||||
let binding = &self.bindings[binding_id];
|
||||
match &binding.kind {
|
||||
// Ex) Given `module="sys"` and `object="exit"`:
|
||||
|
@ -987,7 +987,22 @@ impl<'a> SemanticModel<'a> {
|
|||
_ => {}
|
||||
}
|
||||
None
|
||||
})
|
||||
});
|
||||
|
||||
let first = imported_names.next()?;
|
||||
if let Some(second) = imported_names.next() {
|
||||
// Multiple candidates. We need to sort them because `scope.bindings()` is a HashMap
|
||||
// which doesn't have a stable iteration order.
|
||||
|
||||
let mut imports: Vec<_> =
|
||||
[first, second].into_iter().chain(imported_names).collect();
|
||||
imports.sort_unstable_by_key(|import| import.range.start());
|
||||
|
||||
// Return the binding that was imported last.
|
||||
imports.pop()
|
||||
} else {
|
||||
Some(first)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue