From b34448484f2e35966613f3ee4041e382ddae9ad5 Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Thu, 20 Nov 2014 16:04:56 +0300 Subject: [PATCH] Fixed useless 'import resolve to its containing file' for import statements in __init__.py (PY-14398) --- .../PyUnresolvedReferencesInspection.java | 11 +---------- .../ImportToContainingFileInPackage/p1/__init__.py | 1 + .../ImportToContainingFileInPackage/p1/m1.py | 0 .../PyUnresolvedReferencesInspectionTest.java | 5 +++++ 4 files changed, 7 insertions(+), 10 deletions(-) create mode 100644 python/testData/inspections/PyUnresolvedReferencesInspection/ImportToContainingFileInPackage/p1/__init__.py create mode 100644 python/testData/inspections/PyUnresolvedReferencesInspection/ImportToContainingFileInPackage/p1/m1.py diff --git a/python/src/com/jetbrains/python/inspections/unresolvedReference/PyUnresolvedReferencesInspection.java b/python/src/com/jetbrains/python/inspections/unresolvedReference/PyUnresolvedReferencesInspection.java index 37ff6c0afc0b..5f6961bad551 100644 --- a/python/src/com/jetbrains/python/inspections/unresolvedReference/PyUnresolvedReferencesInspection.java +++ b/python/src/com/jetbrains/python/inspections/unresolvedReference/PyUnresolvedReferencesInspection.java @@ -383,16 +383,7 @@ public class PyUnresolvedReferencesInspection extends PyInspection { } private static boolean isContainingFileImportAllowed(PyElement node, PsiFile target) { - // import resolving to containing file is allowed when we're importing from the current package and the containing file - // is __init__.py (PY-5265) - final boolean insideFromImport = PsiTreeUtil.getParentOfType(node, PyFromImportStatement.class) != null; - if (!insideFromImport) { - return false; - } - if (PyImportStatementNavigator.getImportStatementByElement(node) != null) { - return false; - } - return target.getName().equals(PyNames.INIT_DOT_PY); + return PyImportStatementNavigator.getImportStatementByElement(node) == null && target.getName().equals(PyNames.INIT_DOT_PY); } private void processReferenceInImportGuard(PyElement node, PyExceptPart guard) { diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/ImportToContainingFileInPackage/p1/__init__.py b/python/testData/inspections/PyUnresolvedReferencesInspection/ImportToContainingFileInPackage/p1/__init__.py new file mode 100644 index 000000000000..f72078f20b5c --- /dev/null +++ b/python/testData/inspections/PyUnresolvedReferencesInspection/ImportToContainingFileInPackage/p1/__init__.py @@ -0,0 +1 @@ +import p1.m1 diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/ImportToContainingFileInPackage/p1/m1.py b/python/testData/inspections/PyUnresolvedReferencesInspection/ImportToContainingFileInPackage/p1/m1.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java index 35097e0ea6c3..5367412c133e 100644 --- a/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java @@ -435,6 +435,11 @@ public class PyUnresolvedReferencesInspectionTest extends PyInspectionTestCase { doTest(); } + // PY-14398 + public void testImportToContainingFileInPackage() { + doMultiFileTest("p1/__init__.py"); + } + @NotNull @Override protected Class getInspectionClass() {