PY-19674 Treat all relative (with dots) "from" imports as application local imports
authorMikhail Golubev <mikhail.golubev@jetbrains.com>
Fri, 27 May 2016 12:47:49 +0000 (15:47 +0300)
committerMikhail Golubev <mikhail.golubev@jetbrains.com>
Wed, 15 Jun 2016 16:34:13 +0000 (19:34 +0300)
python/src/com/jetbrains/python/codeInsight/imports/AddImportHelper.java
python/testData/optimizeImports/alphabeticalOrder.after.py
python/testData/optimizeImports/disableAlphabeticalOrder.after.py
python/testData/optimizeImports/unresolvedRelativeImportsShouldBeInProjectGroup/pkg/__init__.py [new file with mode: 0644]
python/testData/optimizeImports/unresolvedRelativeImportsShouldBeInProjectGroup/pkg/main.after.py [new file with mode: 0644]
python/testData/optimizeImports/unresolvedRelativeImportsShouldBeInProjectGroup/pkg/main.py [new file with mode: 0644]
python/testSrc/com/jetbrains/python/PyOptimizeImportsTest.java

index ba0f11131b2c464c5e4daed0c9999c13c33b285e..52ca2006a44cd27de9d0c3ffb10c7a9217f34981 100644 (file)
@@ -225,6 +225,9 @@ public class AddImportHelper {
       if (fromImportStatement.isFromFuture()) {
         return ImportPriority.FUTURE;
       }
+      if (fromImportStatement.getRelativeLevel() > 0) {
+        return ImportPriority.PROJECT;
+      }
       resolved = fromImportStatement.resolveImportSource();
     }
     else {
index edf016f2f3335e3db12addad9078af0a8c30bddd..b06b7ad267a8a2d0964fadd5bf38e24767530a74 100644 (file)
@@ -16,6 +16,7 @@ from alphabet import B, A
 from alphabet import C
 from alphabet import D
 from b import func
+
 from . import m1
 from . import m4, m5
 from .pkg import m3
index 46e2c5d80bbe7411dfd9899b7135157a5ccac924..25e330235b74a5a895966c9dabc34acfcc12a0f0 100644 (file)
@@ -12,12 +12,13 @@ from alphabet import D
 from b import func
 from
 import foo # broken
-from . import m1
 import # broken
 from alphabet import *
-from .. import m2
 from alphabet import C
 from alphabet import B, A
+
+from . import m1
+from .. import m2
 from .pkg import m3
 from . import m4, m5
 
diff --git a/python/testData/optimizeImports/unresolvedRelativeImportsShouldBeInProjectGroup/pkg/__init__.py b/python/testData/optimizeImports/unresolvedRelativeImportsShouldBeInProjectGroup/pkg/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/python/testData/optimizeImports/unresolvedRelativeImportsShouldBeInProjectGroup/pkg/main.after.py b/python/testData/optimizeImports/unresolvedRelativeImportsShouldBeInProjectGroup/pkg/main.after.py
new file mode 100644 (file)
index 0000000..ba77b50
--- /dev/null
@@ -0,0 +1,6 @@
+import re
+
+from . import foo
+from .module import A
+
+print(re, foo, A)
diff --git a/python/testData/optimizeImports/unresolvedRelativeImportsShouldBeInProjectGroup/pkg/main.py b/python/testData/optimizeImports/unresolvedRelativeImportsShouldBeInProjectGroup/pkg/main.py
new file mode 100644 (file)
index 0000000..47d1a0c
--- /dev/null
@@ -0,0 +1,5 @@
+import re
+from . import foo
+from .module import A
+
+print(re, foo, A)
index 251a49d8560d44a795ae3d2e0b17b02839adf9fe..3226eb60ae5ab1dbbedc617c026fc1a7368f7518 100644 (file)
@@ -182,6 +182,15 @@ public class PyOptimizeImportsTest extends PyTestCase {
     doTest();
   }
 
+  // PY-19674
+  public void testUnresolvedRelativeImportsShouldBeInProjectGroup() {
+    final String testName = getTestName(true);
+    myFixture.copyDirectoryToProject(testName, "");
+    myFixture.configureByFile("pkg/main.py");
+    OptimizeImportsAction.actionPerformedImpl(DataManager.getInstance().getDataContext(myFixture.getEditor().getContentComponent()));
+    myFixture.checkResultByFile(testName + "/pkg/main.after.py");
+  }
+
   private void doTest() {
     myFixture.configureByFile(getTestName(true) + ".py");
     OptimizeImportsAction.actionPerformedImpl(DataManager.getInstance().getDataContext(myFixture.getEditor().getContentComponent()));