Don't collect names in parameter default and annotation for visited function (PY-19599)

Because they are collected in the outer scope and outer scope is considered as their owner in ScopeUtil.getScopeOwner.
This commit is contained in:
Semyon Proshev
2018-04-02 19:01:29 +03:00
parent c80c39e29e
commit 87cfacbb3f
2 changed files with 17 additions and 5 deletions
@@ -236,6 +236,11 @@ public class ScopeImpl implements Scope {
super.visitPyFunction(node);
}
@Override
public void visitPyNamedParameter(PyNamedParameter node) {
processNamedElement(node);
}
@Override
public void visitPyClass(PyClass node) {
visitDecorators(node.getDecoratorList());
@@ -249,11 +254,7 @@ public class ScopeImpl implements Scope {
@Override
public void visitPyElement(PyElement node) {
if (node instanceof PsiNamedElement && !(node instanceof PyKeywordArgument)) {
final String name = node.getName();
if (!namedElements.containsKey(name)) {
namedElements.put(name, Sets.newLinkedHashSet());
}
namedElements.get(name).add((PsiNamedElement)node);
processNamedElement((PsiNamedElement)node);
}
if (node instanceof PyImportedNameDefiner) {
importedNameDefiners.add((PyImportedNameDefiner)node);
@@ -274,6 +275,10 @@ public class ScopeImpl implements Scope {
}
}
}
private void processNamedElement(@NotNull PsiNamedElement element) {
namedElements.computeIfAbsent(element.getName(), __ -> Sets.newLinkedHashSet()).add(element);
}
});
myNamedElements = namedElements;
@@ -714,6 +714,13 @@ public class PyUnresolvedReferencesInspectionTest extends PyInspectionTestCase {
" self.value = self.datetime(2016, 1, 1)");
}
// PY-19599
public void testDefinedInParameterDefaultAndBody() {
doTestByText("def f(p=(x for x in [])):\n" +
" x = 1\n" +
" return x");
}
@NotNull
@Override
protected Class<? extends PyInspection> getInspectionClass() {