PY-21651 Fixed: Unresolved reference for attributes created by with statements

Visit with-statements while collecting targets inside `__int__` and `__new__` methods
This commit is contained in:
Semyon Proshev
2017-01-16 15:52:56 +03:00
parent ea28ccc01d
commit 0e377b5dc6
5 changed files with 43 additions and 1 deletions

View File

@@ -1214,6 +1214,15 @@ public class PyClassImpl extends PyBaseElementImpl<PyClassStub> implements PyCla
}
}
}
@Override
public void visitPyWithStatement(PyWithStatement node) {
StreamEx
.of(node.getWithItems())
.map(PyWithItem::getTarget)
.select(PyTargetExpression.class)
.forEach(result::add);
}
});
return result;
}

View File

@@ -0,0 +1,3 @@
from foo import Foo
print(Foo().scope)

View File

@@ -0,0 +1,4 @@
class Foo(object):
def __init__(self):
with open('scope') as self.scope:
pass

View File

@@ -0,0 +1,7 @@
class Foo(object):
def __init__(self):
with open('scope') as self.scope:
pass
def get_scope(self):
return self.scope

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,11 +19,14 @@ import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.util.JDOMUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.xdebugger.impl.XSourcePositionImpl;
import com.jetbrains.python.debugger.PyDebuggerEditorsProvider;
import com.jetbrains.python.fixtures.PyInspectionTestCase;
import com.jetbrains.python.inspections.unresolvedReference.PyUnresolvedReferencesInspection;
import com.jetbrains.python.psi.LanguageLevel;
import com.jetbrains.python.psi.PyFile;
import com.jetbrains.python.psi.impl.PyExpressionCodeFragmentImpl;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
@@ -744,6 +747,22 @@ public class PyUnresolvedReferencesInspectionTest extends PyInspectionTestCase {
doTest();
}
// PY-21651
public void testInstanceAttributeCreatedThroughWithStatement() {
doTest();
}
// PY-21651
public void testInstanceAttributeCreatedThroughWithStatementInAnotherFile() {
doMultiFileTest();
final VirtualFile fooVFile = myFixture.getFile().getVirtualFile().getParent().getChildren()[1];
assertEquals("foo.py", fooVFile.getName());
final PsiFile fooPsiFile = PsiManager.getInstance(myFixture.getProject()).findFile(fooVFile);
assertNotParsed((PyFile)fooPsiFile);
}
@NotNull
@Override
protected Class<? extends PyInspection> getInspectionClass() {