From 37f6807f0266fb653812fa4222baf1e6310689ad Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Sat, 2 Aug 2014 01:25:53 +0400 Subject: [PATCH] Fixed resolving star imports for nested packages with __all__ (PY-13585) --- python/src/com/jetbrains/python/psi/impl/PyFileImpl.java | 8 ++++++-- .../UnusedImportBeforeStarDunderAll/a.py | 3 +++ .../UnusedImportBeforeStarDunderAll/b.py | 0 .../UnusedImportBeforeStarDunderAll/p1/__init__.py | 1 + .../UnusedImportBeforeStarDunderAll/p1/m1.py | 2 ++ .../inspections/PyUnresolvedReferencesInspectionTest.java | 5 +++++ 6 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 python/testData/inspections/PyUnresolvedReferencesInspection/UnusedImportBeforeStarDunderAll/a.py create mode 100644 python/testData/inspections/PyUnresolvedReferencesInspection/UnusedImportBeforeStarDunderAll/b.py create mode 100644 python/testData/inspections/PyUnresolvedReferencesInspection/UnusedImportBeforeStarDunderAll/p1/__init__.py create mode 100644 python/testData/inspections/PyUnresolvedReferencesInspection/UnusedImportBeforeStarDunderAll/p1/m1.py diff --git a/python/src/com/jetbrains/python/psi/impl/PyFileImpl.java b/python/src/com/jetbrains/python/psi/impl/PyFileImpl.java index 030fcfe3d7e5..1040c1620a23 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyFileImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyFileImpl.java @@ -477,9 +477,13 @@ public class PyFileImpl extends PsiFileBase implements PyFile, PyExpression { if (starImportSource != null) { starImportSource = PyUtil.turnDirIntoInit(starImportSource); if (starImportSource instanceof PyFile) { - final PsiElement result = ((PyFile)starImportSource).getElementNamed(name); + final PyFile file = (PyFile)starImportSource; + final PsiElement result = file.getElementNamed(name); if (result != null) { - return result; + final List dunderAll = file.getDunderAll(); + if ((dunderAll == null && !name.startsWith("_")) || (dunderAll != null && dunderAll.contains(name))) { + return result; + } } } } diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/UnusedImportBeforeStarDunderAll/a.py b/python/testData/inspections/PyUnresolvedReferencesInspection/UnusedImportBeforeStarDunderAll/a.py new file mode 100644 index 000000000000..6d444960ee9d --- /dev/null +++ b/python/testData/inspections/PyUnresolvedReferencesInspection/UnusedImportBeforeStarDunderAll/a.py @@ -0,0 +1,3 @@ +import b +from p1 import * +print(b) diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/UnusedImportBeforeStarDunderAll/b.py b/python/testData/inspections/PyUnresolvedReferencesInspection/UnusedImportBeforeStarDunderAll/b.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/UnusedImportBeforeStarDunderAll/p1/__init__.py b/python/testData/inspections/PyUnresolvedReferencesInspection/UnusedImportBeforeStarDunderAll/p1/__init__.py new file mode 100644 index 000000000000..31f7e1aa969c --- /dev/null +++ b/python/testData/inspections/PyUnresolvedReferencesInspection/UnusedImportBeforeStarDunderAll/p1/__init__.py @@ -0,0 +1 @@ +from p1.m1 import * diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/UnusedImportBeforeStarDunderAll/p1/m1.py b/python/testData/inspections/PyUnresolvedReferencesInspection/UnusedImportBeforeStarDunderAll/p1/m1.py new file mode 100644 index 000000000000..3f8018db03f2 --- /dev/null +++ b/python/testData/inspections/PyUnresolvedReferencesInspection/UnusedImportBeforeStarDunderAll/p1/m1.py @@ -0,0 +1,2 @@ +import b +__all__ = [] diff --git a/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java index 176708421fcd..a0a2a1232b2e 100644 --- a/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java @@ -376,6 +376,11 @@ public class PyUnresolvedReferencesInspectionTest extends PyInspectionTestCase { doMultiFileTest(); } + // PY-13585 + public void testUnusedImportBeforeStarDunderAll() { + doMultiFileTest(); + } + @NotNull @Override protected Class getInspectionClass() {