PY-61591 The scope of a class' variable does not extend to contained blocks

GitOrigin-RevId: 871bb3584f9c72f91aced9e86909feccf0a901d7
This commit is contained in:
Petr
2025-09-05 18:12:43 +00:00
committed by intellij-monorepo-bot
parent 059a568ad0
commit 3affb20872
4 changed files with 25 additions and 3 deletions
@@ -736,9 +736,11 @@ public final class PyExtractMethodUtil {
myFunction = s -> {
ScopeOwner owner = parent;
while (owner != null) {
final Scope scope = ControlFlowCache.getScope(owner);
if (scope.containsDeclaration(s)) {
return false;
if (!(owner instanceof PyClass)) {
final Scope scope = ControlFlowCache.getScope(owner);
if (scope.containsDeclaration(s)) {
return false;
}
}
owner = ScopeUtil.getScopeOwner(owner);
}
@@ -0,0 +1,9 @@
class A:
def foo(self): ...
def bar(self):
def baz():
foo()
def foo():
print('baz')
@@ -0,0 +1,6 @@
class A:
def foo(self): ...
def bar(self):
def baz():
<selection>print('baz')</selection>
@@ -391,4 +391,9 @@ public class PyExtractMethodTest extends LightMarkedTestCase {
public void testMethodNameCanShadowModuleFunction() {
doTest("_require_instance");
}
// PY-61591
public void testInnerFunctionWithSameNameAsOuterMethod() {
doTest("foo");
}
}