If smth is listed in __all__ but not found, add module to results. (PY-26243, PY-25751)

This commit is contained in:
Semyon Proshev
2017-10-31 21:23:58 +03:00
parent 925a28b8bd
commit 45beb4e273
12 changed files with 44 additions and 44 deletions

View File

@@ -1,35 +1,18 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.jetbrains.python.codeInsight;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiNamedElement;
import com.intellij.psi.PsiReferenceBase;
import com.intellij.psi.ResolveState;
import com.intellij.psi.*;
import com.intellij.psi.scope.BaseScopeProcessor;
import com.intellij.util.ArrayUtil;
import com.intellij.util.containers.ContainerUtil;
import com.jetbrains.python.PyNames;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.impl.LightNamedElement;
import com.jetbrains.python.psi.types.PyModuleType;
import com.jetbrains.python.psi.resolve.RatedResolveResult;
import com.jetbrains.python.psi.types.PyModuleType;
import one.util.streamex.StreamEx;
import org.jetbrains.annotations.NotNull;
@@ -41,7 +24,7 @@ import java.util.Set;
/**
* @author yole
*/
public class PyDunderAllReference extends PsiReferenceBase<PyStringLiteralExpression> {
public class PyDunderAllReference extends PsiPolyVariantReferenceBase<PyStringLiteralExpression> {
public PyDunderAllReference(@NotNull PyStringLiteralExpression element) {
super(element);
final List<TextRange> ranges = element.getStringValueTextRanges();
@@ -51,12 +34,13 @@ public class PyDunderAllReference extends PsiReferenceBase<PyStringLiteralExpres
}
@Override
public PsiElement resolve() {
@NotNull
public ResolveResult[] multiResolve(boolean incompleteCode) {
final PyStringLiteralExpression element = getElement();
final String name = element.getStringValue();
final PyFile file = (PyFile)element.getContainingFile();
final List<RatedResolveResult> resolveResults = PyUtil.filterTopPriorityResults(file.multiResolveName(name));
final List<RatedResolveResult> resolveResults = file.multiResolveName(name);
final boolean onlyDunderAlls = StreamEx
.of(resolveResults)
@@ -64,11 +48,9 @@ public class PyDunderAllReference extends PsiReferenceBase<PyStringLiteralExpres
.allMatch(resolvedElement -> resolvedElement instanceof PyTargetExpression &&
PyNames.ALL.equals(((PyTargetExpression)resolvedElement).getName()));
if (onlyDunderAlls) return null;
if (onlyDunderAlls) return ResolveResult.EMPTY_ARRAY;
final RatedResolveResult resolveResult = ContainerUtil.getFirstItem(resolveResults);
if (resolveResult == null) return null;
return resolveResult.getElement();
return ContainerUtil.toArray(resolveResults, RatedResolveResult.EMPTY_ARRAY);
}
@NotNull

View File

@@ -1,18 +1,4 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.jetbrains.python.psi.impl;
import com.google.common.collect.Lists;
@@ -426,6 +412,11 @@ public class PyFileImpl extends PsiFileBase implements PyFile, PyExpression {
final PsiElement allElement = findExportedName(PyNames.ALL);
final ResolveResultList allFallbackResults = new ResolveResultList();
allFallbackResults.poke(allElement, RatedResolveResult.RATE_LOW);
PyResolveImportUtil
.resolveModuleAt(QualifiedName.fromComponents(name), getContainingDirectory(), PyResolveImportUtil.fromFoothold(this))
.forEach(module -> allFallbackResults.poke(module, RatedResolveResult.RATE_LOW));
return allFallbackResults;
}
return Collections.emptyList();

View File

@@ -0,0 +1 @@
__all__ = ["aaa"]

View File

@@ -0,0 +1 @@
__all__ = ["aaa"]

View File

@@ -2302,6 +2302,20 @@ public class PyTypeTest extends PyTestCase {
);
}
// PY-25751
public void testNotImportedModuleInDunderAll() {
doMultiFileTest("Union[aaa.py, Any]",
"from pkg import *\n" +
"expr = aaa");
}
// PY-25751
public void testNotImportedPackageInDunderAll() {
doMultiFileTest("Union[__init__.py, Any]",
"from pkg import *\n" +
"expr = aaa");
}
// PY-26269
public void testDontReplaceDictValueWithReceiverType() {
runWithLanguageLevel(

View File

@@ -12,7 +12,6 @@ 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;
@@ -571,7 +570,7 @@ public class PyUnresolvedReferencesInspectionTest extends PyInspectionTestCase {
assertEquals("foo.py", fooVFile.getName());
final PsiFile fooPsiFile = PsiManager.getInstance(myFixture.getProject()).findFile(fooVFile);
assertNotParsed((PyFile)fooPsiFile);
assertNotParsed(fooPsiFile);
}
// PY-23164
@@ -648,6 +647,16 @@ public class PyUnresolvedReferencesInspectionTest extends PyInspectionTestCase {
doTest();
}
// PY-26243
public void testNotImportedModuleInDunderAll() {
doMultiFileTest("pkg/__init__.py");
}
// PY-26243
public void testNotImportedPackageInDunderAll() {
doMultiFileTest("pkg/__init__.py");
}
@NotNull
@Override
protected Class<? extends PyInspection> getInspectionClass() {