diff --git a/python/src/com/jetbrains/python/inspections/PyProtectedMemberInspection.java b/python/src/com/jetbrains/python/inspections/PyProtectedMemberInspection.java
index 72ab466113bb..a6b6e9603696 100644
--- a/python/src/com/jetbrains/python/inspections/PyProtectedMemberInspection.java
+++ b/python/src/com/jetbrains/python/inspections/PyProtectedMemberInspection.java
@@ -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)
diff --git a/python/testSrc/com/jetbrains/python/inspections/PyProtectedMemberInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/PyProtectedMemberInspectionTest.java
index 05b3386f936e..575bf108426d 100644
--- a/python/testSrc/com/jetbrains/python/inspections/PyProtectedMemberInspectionTest.java
+++ b/python/testSrc/com/jetbrains/python/inspections/PyProtectedMemberInspectionTest.java
@@ -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(a._arg)\n" +
+ "print(a._f())")
+ );
+ }
+
@NotNull
@Override
protected Class extends PyInspection> getInspectionClass() {