mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:24:57 +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():
|
def f():
|
||||||
if any((key := (value := x)) for x in ["ok"]):
|
if any((key := (value := x)) for x in ["ok"]):
|
||||||
print(key)
|
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
|
// 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()) {
|
for (name, range) in names.iter().zip(ranges.iter()) {
|
||||||
let binding_id = self
|
let binding_id = self
|
||||||
.semantic_model
|
.semantic_model
|
||||||
.scopes
|
.scopes
|
||||||
.ancestors(self.semantic_model.scope_id)
|
.ancestors(self.semantic_model.scope_id)
|
||||||
.skip(1)
|
.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()));
|
.find_map(|scope| scope.get(name.as_str()));
|
||||||
|
|
||||||
if let Some(binding_id) = binding_id {
|
if let Some(binding_id) = binding_id {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue