Commit d9b5e61d authored by Valentina Kiryushkina's avatar Valentina Kiryushkina
Browse files

PY-14234 Fix false positive when underscored module imported inside the package

parent 2e2eed7f
Showing with 26 additions and 1 deletion
+26 -1
...@@ -21,6 +21,7 @@ import com.intellij.codeInspection.ProblemHighlightType; ...@@ -21,6 +21,7 @@ import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder; import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel; import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel;
import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor; import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiReference; import com.intellij.psi.PsiReference;
...@@ -85,8 +86,18 @@ public class PyProtectedMemberInspection extends PyInspection { ...@@ -85,8 +86,18 @@ public class PyProtectedMemberInspection extends PyInspection {
if (!(statement instanceof PyFromImportStatement)) return; if (!(statement instanceof PyFromImportStatement)) return;
final PyReferenceExpression importReferenceExpression = node.getImportReferenceExpression(); final PyReferenceExpression importReferenceExpression = node.getImportReferenceExpression();
final PyReferenceExpression importSource = ((PyFromImportStatement)statement).getImportSource(); final PyReferenceExpression importSource = ((PyFromImportStatement)statement).getImportSource();
if (importReferenceExpression != null && importSource != null) if (importReferenceExpression != null && importSource != null && !isImportFromTheSamePackage(importSource)) {
checkReference(importReferenceExpression, importSource); checkReference(importReferenceExpression, importSource);
}
}
private boolean isImportFromTheSamePackage(PyReferenceExpression importSource) {
PsiDirectory directory = importSource.getContainingFile().getContainingDirectory();
if (PyUtil.isPackage(directory, true, importSource.getContainingFile()) &&
directory.getName().equals(importSource.getName())) {
return true;
}
return false;
} }
@Override @Override
......
def very_smart_func(param):
return None
\ No newline at end of file
from my_package import _package_internal_module # False positive here
_package_internal_module.very_smart_func("spam")
\ No newline at end of file
...@@ -69,6 +69,15 @@ public class PyProtectedMemberInspectionTest extends PyTestCase { ...@@ -69,6 +69,15 @@ public class PyProtectedMemberInspectionTest extends PyTestCase {
myFixture.checkHighlighting(false, false, true); myFixture.checkHighlighting(false, false, true);
} }
//PY-14234
public void testImportFromTheSamePackage() {
String path = getTestName(true);
myFixture.copyDirectoryToProject(path + "/my_package", "./my_package");
myFixture.configureByFile("/my_package/my_public_module.py");
myFixture.enableInspections(PyProtectedMemberInspection.class);
myFixture.checkHighlighting(false, false, true);
}
public void testModule() { public void testModule() {
myFixture.configureByFiles(getTestName(true) + ".py", "tmp.py"); myFixture.configureByFiles(getTestName(true) + ".py", "tmp.py");
myFixture.enableInspections(PyProtectedMemberInspection.class); myFixture.enableInspections(PyProtectedMemberInspection.class);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment