From d93debdfda02917a36336fa63e201b6ee3791932 Mon Sep 17 00:00:00 2001 From: Semyon Proshev Date: Tue, 17 Oct 2017 19:35:27 +0300 Subject: [PATCH] Pass type eval context to PyInspectionExtension.ignoreUnresolvedReference --- .../PyStudyInspectionExtension.java | 5 ++- .../inspections/PyInspectionExtension.java | 37 +++++++++++-------- .../stdlib/PyStdlibInspectionExtension.kt | 4 +- .../PyUnresolvedReferencesInspection.java | 2 +- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/python/educational-python/Edu-Python/src/com/jetbrains/edu/learning/highlighting/PyStudyInspectionExtension.java b/python/educational-python/Edu-Python/src/com/jetbrains/edu/learning/highlighting/PyStudyInspectionExtension.java index 66f627c5e37c..86c67130a773 100644 --- a/python/educational-python/Edu-Python/src/com/jetbrains/edu/learning/highlighting/PyStudyInspectionExtension.java +++ b/python/educational-python/Edu-Python/src/com/jetbrains/edu/learning/highlighting/PyStudyInspectionExtension.java @@ -4,18 +4,19 @@ import com.intellij.openapi.project.Project; import com.intellij.psi.PsiFile; import com.intellij.psi.PsiReference; import com.intellij.psi.util.PsiTreeUtil; -import com.jetbrains.edu.learning.courseFormat.TaskFile; import com.jetbrains.edu.learning.StudyTaskManager; import com.jetbrains.edu.learning.StudyUtils; +import com.jetbrains.edu.learning.courseFormat.TaskFile; import com.jetbrains.python.inspections.PyInspectionExtension; import com.jetbrains.python.psi.PyElement; import com.jetbrains.python.psi.PyImportStatementBase; +import com.jetbrains.python.psi.types.TypeEvalContext; import org.jetbrains.annotations.NotNull; public class PyStudyInspectionExtension extends PyInspectionExtension { @Override - public boolean ignoreUnresolvedReference(@NotNull PyElement element, @NotNull PsiReference reference) { + public boolean ignoreUnresolvedReference(@NotNull PyElement element, @NotNull PsiReference reference, @NotNull TypeEvalContext context) { final PsiFile file = element.getContainingFile(); final Project project = file.getProject(); diff --git a/python/psi-api/src/com/jetbrains/python/inspections/PyInspectionExtension.java b/python/psi-api/src/com/jetbrains/python/inspections/PyInspectionExtension.java index 63aeea850ca5..3dbc5de69a57 100644 --- a/python/psi-api/src/com/jetbrains/python/inspections/PyInspectionExtension.java +++ b/python/psi-api/src/com/jetbrains/python/inspections/PyInspectionExtension.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.jetbrains.python.inspections; import com.intellij.openapi.extensions.ExtensionPointName; @@ -51,7 +37,28 @@ public abstract class PyInspectionExtension { return false; } + /** + * Checks if unresolved reference could be ignored. + * + * @param node element containing reference + * @param reference unresolved reference + * @return true if the unresolved reference could be ignored + * @deprecated Use {@link PyInspectionExtension#ignoreUnresolvedReference(PyElement, PsiReference, TypeEvalContext)} instead. + * This method will be remove in 2018.2. + */ + @Deprecated public boolean ignoreUnresolvedReference(@NotNull PyElement node, @NotNull PsiReference reference) { + return ignoreUnresolvedReference(node, reference, TypeEvalContext.codeInsightFallback(node.getProject())); + } + + /** + * Checks if unresolved reference could be ignored. + * + * @param node element containing reference + * @param reference unresolved reference + * @return true if the unresolved reference could be ignored + */ + public boolean ignoreUnresolvedReference(@NotNull PyElement node, @NotNull PsiReference reference, @NotNull TypeEvalContext context) { return false; } diff --git a/python/src/com/jetbrains/python/inspections/stdlib/PyStdlibInspectionExtension.kt b/python/src/com/jetbrains/python/inspections/stdlib/PyStdlibInspectionExtension.kt index 181a090d2aad..f9d4b3a9c2e1 100644 --- a/python/src/com/jetbrains/python/inspections/stdlib/PyStdlibInspectionExtension.kt +++ b/python/src/com/jetbrains/python/inspections/stdlib/PyStdlibInspectionExtension.kt @@ -28,11 +28,11 @@ class PyStdlibInspectionExtension : PyInspectionExtension() { return false } - override fun ignoreUnresolvedReference(node: PyElement, reference: PsiReference): Boolean { + override fun ignoreUnresolvedReference(node: PyElement, reference: PsiReference, context: TypeEvalContext): Boolean { if (node is PyReferenceExpression && node.isQualified) { val qualifier = node.qualifier if (qualifier is PyReferenceExpression) { - return PyStdlibClassMembersProvider.referenceToMockPatch(qualifier, TypeEvalContext.codeInsightFallback(qualifier.project)) && + return PyStdlibClassMembersProvider.referenceToMockPatch(qualifier, context) && PyStdlibClassMembersProvider.MOCK_PATCH_MEMBERS.find { it.name == node.name } != null } } diff --git a/python/src/com/jetbrains/python/inspections/unresolvedReference/PyUnresolvedReferencesInspection.java b/python/src/com/jetbrains/python/inspections/unresolvedReference/PyUnresolvedReferencesInspection.java index 746812997858..79c036763b30 100644 --- a/python/src/com/jetbrains/python/inspections/unresolvedReference/PyUnresolvedReferencesInspection.java +++ b/python/src/com/jetbrains/python/inspections/unresolvedReference/PyUnresolvedReferencesInspection.java @@ -380,7 +380,7 @@ public class PyUnresolvedReferencesInspection extends PyInspection { if (unresolved) { boolean ignoreUnresolved = false; for (PyInspectionExtension extension : Extensions.getExtensions(PyInspectionExtension.EP_NAME)) { - if (extension.ignoreUnresolvedReference(node, reference)) { + if (extension.ignoreUnresolvedReference(node, reference, myTypeEvalContext)) { ignoreUnresolved = true; break; }