From 59c29e8ac1fe1091f6c1c4de3e142257aef5605f Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Wed, 28 Sep 2016 19:12:56 +0300 Subject: [PATCH 1/2] PY-18788 Remove redundant test as we first need to improve annotation parsing Specifically, we need to be able to parse annotations in text form and recognize those names that require additional import from "typing" module. Current implementation of PyTypeParser is not capable of that, since it omits types which names cannot be resolved in the context surrounding the type hint. This test was added preemptively, but it's going to take longer than expected to fix the problem, because we want to implement the aforementioned improvement in PyTypeParser as part of storing type annotations in stub files (see PY-18816). --- .../PyAnnotateTypesIntentionTest.java | 30 ------------------- 1 file changed, 30 deletions(-) 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); From e612272559ff7cc5dc0e9f9642717d00952259f0 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Wed, 28 Sep 2016 20:06:02 +0300 Subject: [PATCH 2/2] Fix test: when sorting import candidates use qualified names as tie breaker if lengths of both qualified names are the same. Previously their order and hence the candidate selected in tests were unpredictable due to this flaw. --- .../imports/ImportCandidateHolder.java | 17 ++++++++++------- .../main_after.py | 2 +- .../quickFixes/AddImportQuickFixTest.java | 2 +- 3 files changed, 12 insertions(+), 9 deletions(-) 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/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; }); }