diff --git a/python/src/com/jetbrains/python/inspections/unresolvedReference/PyUnresolvedReferencesInspection.java b/python/src/com/jetbrains/python/inspections/unresolvedReference/PyUnresolvedReferencesInspection.java index c14ddd476427..c32f047ea722 100644 --- a/python/src/com/jetbrains/python/inspections/unresolvedReference/PyUnresolvedReferencesInspection.java +++ b/python/src/com/jetbrains/python/inspections/unresolvedReference/PyUnresolvedReferencesInspection.java @@ -534,10 +534,18 @@ public class PyUnresolvedReferencesInspection extends PyInspection { return; } addCreateMemberFromUsageFixes(type, reference, refText, actions); - if (type instanceof PyClassTypeImpl) { + if (type instanceof PyClassType) { if (reference instanceof PyOperatorReference) { + String className = type.getName(); + final PyClassType classType = (PyClassType)type; + if (classType.isDefinition()) { + final PyClassLikeType metaClassType = classType.getMetaClassType(myTypeEvalContext, true); + if (metaClassType != null) { + className = metaClassType.getName(); + } + } description = PyBundle.message("INSP.unresolved.operator.ref", - type.getName(), refName, + className, refName, ((PyOperatorReference)reference).getReadableOperatorName()); } else { diff --git a/python/src/com/jetbrains/python/psi/impl/references/PyOperatorReference.java b/python/src/com/jetbrains/python/psi/impl/references/PyOperatorReference.java index 9a6ff12a12df..d1c43ee1a4ec 100644 --- a/python/src/com/jetbrains/python/psi/impl/references/PyOperatorReference.java +++ b/python/src/com/jetbrains/python/psi/impl/references/PyOperatorReference.java @@ -22,6 +22,7 @@ import com.jetbrains.python.PyNames; import com.jetbrains.python.psi.*; import com.jetbrains.python.psi.resolve.PyResolveContext; import com.jetbrains.python.psi.resolve.RatedResolveResult; +import com.jetbrains.python.psi.types.PyClassLikeType; import com.jetbrains.python.psi.types.PyClassType; import com.jetbrains.python.psi.types.PyType; import com.jetbrains.python.psi.types.TypeEvalContext; @@ -142,8 +143,13 @@ public class PyOperatorReference extends PyReferenceImpl { final ArrayList results = new ArrayList(); if (object != null && name != null) { final TypeEvalContext typeEvalContext = myContext.getTypeEvalContext(); - final PyType type = typeEvalContext.getType(object); + PyType type = typeEvalContext.getType(object); typeEvalContext.trace("Side text is %s, type is %s", object.getText(), type); + if (type instanceof PyClassLikeType) { + if (((PyClassLikeType)type).isDefinition()) { + type = ((PyClassLikeType)type).getMetaClassType(typeEvalContext, true); + } + } if (type != null) { List res = type.resolveMember(name, object, AccessDirection.of(myElement), myContext); if (res != null && res.size() > 0) { diff --git a/python/testData/inspections/PyTypeCheckerInspection/TypingListSubscriptionExpression.py b/python/testData/inspections/PyTypeCheckerInspection/TypingListSubscriptionExpression.py new file mode 100644 index 000000000000..242a41d363c6 --- /dev/null +++ b/python/testData/inspections/PyTypeCheckerInspection/TypingListSubscriptionExpression.py @@ -0,0 +1,7 @@ +from typing import List, Any + + +def f(x1: List[str], + x2: List['str'], + x3: List[Any]) -> None: + pass diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/unresolvedSubscriptionOnClass.py b/python/testData/inspections/PyUnresolvedReferencesInspection/unresolvedSubscriptionOnClass.py new file mode 100644 index 000000000000..773679e3afdd --- /dev/null +++ b/python/testData/inspections/PyUnresolvedReferencesInspection/unresolvedSubscriptionOnClass.py @@ -0,0 +1,5 @@ +class Foo(object): + def __getitem__(self, item): + return item + +Foo[0] diff --git a/python/testSrc/com/jetbrains/python/inspections/Py3TypeCheckerInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/Py3TypeCheckerInspectionTest.java index 85ca91416a50..b3fc9efa0b0e 100644 --- a/python/testSrc/com/jetbrains/python/inspections/Py3TypeCheckerInspectionTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/Py3TypeCheckerInspectionTest.java @@ -73,4 +73,9 @@ public class Py3TypeCheckerInspectionTest extends PyTestCase { public void testTypingIterableForLoop() { doTest(); } + + // PY-16146 + public void testTypingListSubscriptionExpression() { + doTest(); + } } diff --git a/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java index 59a93465d995..eea37a2e7b6a 100644 --- a/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java @@ -510,7 +510,10 @@ public class PyUnresolvedReferencesInspectionTest extends PyInspectionTestCase { doTest(); } - + // PY-16146 + public void testUnresolvedSubscriptionOnClass() { + doTest(); + } @NotNull @Override