mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 10:23:11 +00:00
Skip class scopes when resolving nonlocal references (#4943)
This commit is contained in:
parent
6950c93934
commit
f17282d615
2 changed files with 21 additions and 2 deletions
|
@ -126,3 +126,22 @@ def f(x: int):
|
|||
def f():
|
||||
if any((key := (value := x)) for x in ["ok"]):
|
||||
print(key)
|
||||
|
||||
|
||||
def f() -> None:
|
||||
is_connected = False
|
||||
|
||||
class Foo:
|
||||
@property
|
||||
def is_connected(self):
|
||||
nonlocal is_connected
|
||||
return is_connected
|
||||
|
||||
def do_thing(self):
|
||||
# This should resolve to the `is_connected` in the function scope.
|
||||
nonlocal is_connected
|
||||
print(is_connected)
|
||||
|
||||
obj = Foo()
|
||||
obj.do_thing()
|
||||
|
||||
|
|
|
@ -291,14 +291,14 @@ where
|
|||
}
|
||||
|
||||
// Mark the binding in the defining scopes as used too. (Skip the global scope
|
||||
// and the current scope.)
|
||||
// and the current scope, and, per standard resolution rules, any class scopes.)
|
||||
for (name, range) in names.iter().zip(ranges.iter()) {
|
||||
let binding_id = self
|
||||
.semantic_model
|
||||
.scopes
|
||||
.ancestors(self.semantic_model.scope_id)
|
||||
.skip(1)
|
||||
.take_while(|scope| !scope.kind.is_module())
|
||||
.filter(|scope| !(scope.kind.is_module() || scope.kind.is_class()))
|
||||
.find_map(|scope| scope.get(name.as_str()));
|
||||
|
||||
if let Some(binding_id) = binding_id {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue