Skip to content

Commit

Permalink
Remove one Scope access
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Apr 29, 2023
1 parent d464e1e commit a5f1ff1
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4267,19 +4267,26 @@ impl<'a> Checker<'a> {
}

// Per [PEP 572](https://peps.python.org/pep-0572/#scope-of-the-target), named
// expressions in generators and comprehensions bind to the parent scope.
// expressions in generators and comprehensions bind to the scope that contains the
// outermost comprehension.
let scope_id = if binding.kind.is_named_expr_assignment() {
self.ctx
.scopes
.ancestor_scopes(self.ctx.scope_id)
.find(|scope| !scope.kind.is_generator())
.expect("Every scope must descend from the global scope.")
.id
.ancestor_ids(self.ctx.scope_id)
.find_map(|scope_id| {
let scope = &self.ctx.scopes[scope_id];
if !scope.kind.is_generator() {
Some(scope_id)
} else {
None
}
})
.unwrap_or(self.ctx.scope_id)
} else {
self.ctx.scope_id
};
let scope = &mut self.ctx.scopes[scope_id];

let scope = &self.ctx.scopes[scope_id];
let binding = if let Some(index) = scope.get(name) {
let existing = &self.ctx.bindings[*index];
match &existing.kind {
Expand Down Expand Up @@ -4317,7 +4324,6 @@ impl<'a> Checker<'a> {
}

// Add the binding to the scope.
let scope = &mut self.ctx.scopes[scope_id];
scope.add(name, binding_id);

// Add the binding to the arena.
Expand Down

0 comments on commit a5f1ff1

Please sign in to comment.