Don't show warnings for unused imports listed in __all__ (PY-2668)

This commit is contained in:
Andrey Vlasovskikh
2012-07-24 14:01:33 +04:00
parent 4552393135
commit 2f4bd0674e
2 changed files with 12 additions and 2 deletions

View File

@@ -715,9 +715,14 @@ public class PyUnresolvedReferencesInspection extends PyInspection {
Set<PyImportStatementBase> unusedStatements = new HashSet<PyImportStatementBase>();
final PyUnresolvedReferencesInspection suppressableInspection = new PyUnresolvedReferencesInspection();
PyQualifiedName packageQName = null;
List<String> dunderAll = null;
for (NameDefiner unusedImport : unusedImports) {
if (packageQName == null) {
final PsiFile file = unusedImport.getContainingFile();
if (file instanceof PyFile) {
dunderAll = ((PyFile)file).getDunderAll();
}
if (file != null && PyUtil.isPackage(file)) {
packageQName = ResolveImportUtil.findShortestImportableQName(file);
}
@@ -745,10 +750,14 @@ public class PyUnresolvedReferencesInspection extends PyInspection {
}
PsiFileSystemItem importedElement;
if (unusedImport instanceof PyImportElement) {
final PsiElement element = ResolveImportUtil.resolveImportElement((PyImportElement)unusedImport);
final PyImportElement importElement = (PyImportElement)unusedImport;
final PsiElement element = ResolveImportUtil.resolveImportElement(importElement);
if (element == null) {
continue;
}
if (dunderAll != null && dunderAll.contains(importElement.getVisibleName())) {
continue;
}
importedElement = element.getContainingFile();
}
else {

View File

@@ -1,6 +1,7 @@
from .m1 import f
from p1.m1 import f
from m1 import f
<warning descr="Unused import statement">from a import g, h</warning>
from a import g
<warning descr="Unused import statement">from a import h</warning>
__all__ = ['f', 'g']