Fix checking if same-package protected module is imported in the inspected one (PY-32485)

It led to false negative when inspected reference resolves to an element in the same file
This commit is contained in:
Semyon Proshev
2018-11-08 14:17:02 +03:00
parent 61b586f8fe
commit 76b77f6af1
2 changed files with 19 additions and 3 deletions
@@ -82,9 +82,7 @@ public class PyProtectedMemberInspection extends PyInspection {
return StreamEx
.of(importSource.getReference(resolveContext).multiResolve(false))
.map(ResolveResult::getElement)
.nonNull()
.map(PsiElement::getContainingFile)
.nonNull()
.select(PyFile.class)
.map(PsiFile::getContainingDirectory)
.nonNull()
.map(PsiDirectory::getVirtualFile)
@@ -94,6 +94,24 @@ public class PyProtectedMemberInspectionTest extends PyInspectionTestCase {
doMultiFileTest("my_package/my_subpackage/module2.py");
}
// PY-32485
public void testProtectedMemberOfSameFileClass() {
// created file should be considered as located inside a package so Python 3 is used here
runWithLanguageLevel(
LanguageLevel.PYTHON34,
() -> doTestByText("class A:\n" +
" def __init__(self, arg):\n" +
" self._arg = arg\n" +
"\n" +
" def _f(self):\n" +
" return self._arg\n" +
"\n" +
"a = A(1)\n" +
"print(<weak_warning descr=\"Access to a protected member _arg of a class\">a._arg</weak_warning>)\n" +
"print(<weak_warning descr=\"Access to a protected member _f of a class\">a._f</weak_warning>())")
);
}
@NotNull
@Override
protected Class<? extends PyInspection> getInspectionClass() {