From f98eca66125bf099d1072a6e5e90c2ebd153d226 Mon Sep 17 00:00:00 2001 From: Semyon Proshev Date: Thu, 28 Sep 2017 15:57:28 +0300 Subject: [PATCH] Always use path to target in AddImportHelper.addImport (PY-25484) --- .../codeInsight/imports/AddImportHelper.java | 22 +++---------------- .../classReexportedThroughDunderAll.after.py | 3 +++ .../classReexportedThroughDunderAll.py | 1 + .../pkg/__init__.py | 4 ++++ .../pkg/bar.py | 5 +++++ .../python/PyClassNameCompletionTest.java | 5 +++++ 6 files changed, 21 insertions(+), 19 deletions(-) create mode 100644 python/testData/completion/className/classReexportedThroughDunderAll/classReexportedThroughDunderAll.after.py create mode 100644 python/testData/completion/className/classReexportedThroughDunderAll/classReexportedThroughDunderAll.py create mode 100644 python/testData/completion/className/classReexportedThroughDunderAll/pkg/__init__.py create mode 100644 python/testData/completion/className/classReexportedThroughDunderAll/pkg/bar.py diff --git a/python/src/com/jetbrains/python/codeInsight/imports/AddImportHelper.java b/python/src/com/jetbrains/python/codeInsight/imports/AddImportHelper.java index 4bec13c8d850..773dcc0dbf9b 100644 --- a/python/src/com/jetbrains/python/codeInsight/imports/AddImportHelper.java +++ b/python/src/com/jetbrains/python/codeInsight/imports/AddImportHelper.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2014 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.imports; import com.intellij.lang.injection.InjectedLanguageManager; @@ -561,18 +547,16 @@ public class AddImportHelper { addImportStatement(file, path, null, priority, element); } else { - final QualifiedName toImportQName = QualifiedNameFinder.findCanonicalImportPath(toImport, element); - if (toImportQName == null) return; if (useQualified) { addImportStatement(file, path, null, priority, element); final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(file.getProject()); final String targetName = PyUtil.getElementNameWithoutExtension(target); - element.replace(elementGenerator.createExpressionFromText(LanguageLevel.forElement(target), toImportQName + "." + targetName)); + element.replace(elementGenerator.createExpressionFromText(LanguageLevel.forElement(target), qName + "." + targetName)); } else { final String name = target.getName(); if (name != null) - addOrUpdateFromImportStatement(file, toImportQName.toString(), name, null, priority, element); + addOrUpdateFromImportStatement(file, qName.toString(), name, null, priority, element); } } } diff --git a/python/testData/completion/className/classReexportedThroughDunderAll/classReexportedThroughDunderAll.after.py b/python/testData/completion/className/classReexportedThroughDunderAll/classReexportedThroughDunderAll.after.py new file mode 100644 index 000000000000..d6f8dd459e5f --- /dev/null +++ b/python/testData/completion/className/classReexportedThroughDunderAll/classReexportedThroughDunderAll.after.py @@ -0,0 +1,3 @@ +from pkg import Foo + +foo = Foo \ No newline at end of file diff --git a/python/testData/completion/className/classReexportedThroughDunderAll/classReexportedThroughDunderAll.py b/python/testData/completion/className/classReexportedThroughDunderAll/classReexportedThroughDunderAll.py new file mode 100644 index 000000000000..101897d11178 --- /dev/null +++ b/python/testData/completion/className/classReexportedThroughDunderAll/classReexportedThroughDunderAll.py @@ -0,0 +1 @@ +foo = Foo \ No newline at end of file diff --git a/python/testData/completion/className/classReexportedThroughDunderAll/pkg/__init__.py b/python/testData/completion/className/classReexportedThroughDunderAll/pkg/__init__.py new file mode 100644 index 000000000000..22a56fb36a93 --- /dev/null +++ b/python/testData/completion/className/classReexportedThroughDunderAll/pkg/__init__.py @@ -0,0 +1,4 @@ +from bar import * + + +__all__ = ["Foo"] \ No newline at end of file diff --git a/python/testData/completion/className/classReexportedThroughDunderAll/pkg/bar.py b/python/testData/completion/className/classReexportedThroughDunderAll/pkg/bar.py new file mode 100644 index 000000000000..4f1c56c99e91 --- /dev/null +++ b/python/testData/completion/className/classReexportedThroughDunderAll/pkg/bar.py @@ -0,0 +1,5 @@ +__all__ = ["Foo"] + + +class Foo: + pass \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PyClassNameCompletionTest.java b/python/testSrc/com/jetbrains/python/PyClassNameCompletionTest.java index 04b5ccedae90..11697b602ed0 100644 --- a/python/testSrc/com/jetbrains/python/PyClassNameCompletionTest.java +++ b/python/testSrc/com/jetbrains/python/PyClassNameCompletionTest.java @@ -79,6 +79,11 @@ public class PyClassNameCompletionTest extends PyTestCase { doTest(); } + // PY-25484 + public void testClassReexportedThroughDunderAll() { + doTest(); + } + private void doTest() { final String path = "/completion/className/" + getTestName(true); myFixture.copyDirectoryToProject(path, "");