diff --git a/python/src/com/jetbrains/python/codeInsight/imports/ImportCandidateHolder.java b/python/src/com/jetbrains/python/codeInsight/imports/ImportCandidateHolder.java index c60178052609..c97941077f4f 100644 --- a/python/src/com/jetbrains/python/codeInsight/imports/ImportCandidateHolder.java +++ b/python/src/com/jetbrains/python/codeInsight/imports/ImportCandidateHolder.java @@ -19,13 +19,13 @@ import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.ProjectFileIndex; import com.intellij.openapi.roots.ProjectRootManager; +import com.intellij.openapi.util.Comparing; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.PsiFileSystemItem; import com.intellij.psi.util.QualifiedName; -import com.intellij.util.Function; import com.intellij.util.containers.ContainerUtil; import com.jetbrains.python.psi.*; import org.jetbrains.annotations.NotNull; @@ -160,17 +160,20 @@ public class ImportCandidateHolder implements Comparable return sb.toString(); } - public int compareTo(@NotNull ImportCandidateHolder rhs) { + public int compareTo(@NotNull ImportCandidateHolder other) { final int lRelevance = getRelevance(); - final int rRelevance = rhs.getRelevance(); + final int rRelevance = other.getRelevance(); if (rRelevance != lRelevance) { return rRelevance - lRelevance; } - // prefer shorter paths - if (myPath != null && rhs.myPath != null) { - return myPath.getComponentCount() - rhs.myPath.getComponentCount(); + if (myPath != null && other.myPath != null) { + // prefer shorter paths + final int lengthDiff = myPath.getComponentCount() - other.myPath.getComponentCount(); + if (lengthDiff != 0) { + return lengthDiff; + } } - return 0; + return Comparing.compare(myPath, other.myPath); } int getRelevance() { diff --git a/python/testData/quickFixes/AddImportQuickFixTest/canonicalNamesFromHigherLevelPackage/main_after.py b/python/testData/quickFixes/AddImportQuickFixTest/canonicalNamesFromHigherLevelPackage/main_after.py index 8683c5a7c50e..5670fc2b31a3 100644 --- a/python/testData/quickFixes/AddImportQuickFixTest/canonicalNamesFromHigherLevelPackage/main_after.py +++ b/python/testData/quickFixes/AddImportQuickFixTest/canonicalNamesFromHigherLevelPackage/main_after.py @@ -1,3 +1,3 @@ -from foo import MyClass +from bar import MyClass MyClass() \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/intentions/PyAnnotateTypesIntentionTest.java b/python/testSrc/com/jetbrains/python/intentions/PyAnnotateTypesIntentionTest.java index 2af8ccece36c..e441c27c649e 100644 --- a/python/testSrc/com/jetbrains/python/intentions/PyAnnotateTypesIntentionTest.java +++ b/python/testSrc/com/jetbrains/python/intentions/PyAnnotateTypesIntentionTest.java @@ -15,16 +15,10 @@ */ package com.jetbrains.python.intentions; -import com.intellij.codeInsight.intention.IntentionAction; import com.jetbrains.python.PyBundle; -import com.jetbrains.python.debugger.PySignature; -import com.jetbrains.python.debugger.PySignatureCacheManager; -import com.jetbrains.python.debugger.PySignatureCacheManagerImpl; import com.jetbrains.python.psi.LanguageLevel; import com.jetbrains.python.psi.impl.PythonLanguageLevelPusher; -import java.io.IOException; - /** * @author traff */ @@ -51,30 +45,6 @@ public class PyAnnotateTypesIntentionTest extends PyIntentionTestCase { public void testTypeComment() { doTest(PyBundle.message("INTN.annotate.types"), LanguageLevel.PYTHON27); } - - public void testImportDict() throws IOException { - PythonLanguageLevelPusher.setForcedLanguageLevel(myFixture.getProject(), LanguageLevel.PYTHON30); - try { - final String testFileName = getTestName(true); - myFixture.configureByFile(testFileName + ".py"); - - String signature = PySignatureCacheManager.signatureToString( - new PySignature(myFixture.getFile().getVirtualFile().getCanonicalPath(), "get_dict").addReturnType("Dict[int, str]")); - PySignatureCacheManagerImpl.CALL_SIGNATURES_ATTRIBUTE.writeAttributeBytes(myFixture.getFile().getVirtualFile(), - signature.getBytes()); - - final IntentionAction intentionAction = myFixture.findSingleIntention(PyBundle.message("INTN.annotate.types")); - assertNotNull(intentionAction); - myFixture.launchAction(intentionAction); - myFixture.checkResultByFile(testFileName + "_after.py", true); - } - finally { - PythonLanguageLevelPusher.setForcedLanguageLevel(myFixture.getProject(), null); - } - - doTest(); - } - private void doTest() { doTest(PyBundle.message("INTN.annotate.types"), LanguageLevel.PYTHON30); diff --git a/python/testSrc/com/jetbrains/python/quickFixes/AddImportQuickFixTest.java b/python/testSrc/com/jetbrains/python/quickFixes/AddImportQuickFixTest.java index 7eef13fc19a5..a24591f86951 100644 --- a/python/testSrc/com/jetbrains/python/quickFixes/AddImportQuickFixTest.java +++ b/python/testSrc/com/jetbrains/python/quickFixes/AddImportQuickFixTest.java @@ -52,7 +52,7 @@ public class AddImportQuickFixTest extends PyQuickFixTestCase { doMultiFileAutoImportTest("Import", fix -> { final List candidates = fix.getCandidates(); final List names = ContainerUtil.map(candidates, c -> c.getPresentableText("MyClass")); - assertSameElements(names, "foo.MyClass", "bar.MyClass"); + assertOrderedEquals(names, "bar.MyClass", "foo.MyClass"); return true; }); }