PY-73107 Usages of @warnings.deprecated inside .pyi stubs are ignored

GitOrigin-RevId: 7682eff8c8ca8ae8241ddee3191add34ab2ac22e
This commit is contained in:
Andrey Vokin
2024-06-17 15:32:12 +02:00
committed by intellij-monorepo-bot
parent 84243e4280
commit f2a322c6d9
6 changed files with 27 additions and 4 deletions

View File

@@ -12,11 +12,10 @@ fun extractDeprecationMessageFromDecorator(element: PyAstDecoratable): String? {
return null
}
val argumentList = deprecatedDecorator.argumentList ?: return null
if (argumentList.arguments.isEmpty()) {
if (deprecatedDecorator.arguments.isEmpty()) {
return null
}
val argument = argumentList.arguments[0] as? PyAstStringLiteralExpression ?: return null
val argument = deprecatedDecorator.arguments[0] as? PyAstStringLiteralExpression ?: return null
return argument.stringValue
}

View File

@@ -27,6 +27,7 @@ import com.jetbrains.python.PyPsiBundle;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.PyKnownDecoratorUtil.KnownDecorator;
import com.jetbrains.python.psi.types.TypeEvalContext;
import com.jetbrains.python.pyi.PyiFile;
import com.jetbrains.python.pyi.PyiUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -75,6 +76,13 @@ public final class PyDeprecationInspection extends PyInspection {
@NlsSafe String deprecationMessage = null;
if (resolveResult instanceof PyDeprecatable deprecatable) {
deprecationMessage = deprecatable.getDeprecationMessage();
if (deprecationMessage == null && !(resolveResult.getContainingFile() instanceof PyiFile)) {
PsiElement stub = PyiUtil.getPythonStub((PyElement)deprecatable);
if (stub instanceof PyDeprecatable stubDeprecatable) {
deprecationMessage = stubDeprecatable.getDeprecationMessage();
}
}
}
else if (resolveResult instanceof PyFile) {
deprecationMessage = ((PyFile)resolveResult).getDeprecationMessage();

View File

@@ -0,0 +1,2 @@
def my_method(x: int) -> int:
return 0

View File

@@ -0,0 +1,5 @@
from typing_extensions import deprecated
@deprecated("deprecated method")
def my_method(x: int) -> int: ...

View File

@@ -0,0 +1,3 @@
from deprecatedLibrary import my_method
<warning descr="deprecated method">my_method</warning>(10)

View File

@@ -114,6 +114,12 @@ public class PyDeprecationTest extends PyTestCase {
myFixture.checkHighlighting(true, false, false);
}
public void testStubAndPyFile() {
myFixture.enableInspections(PyDeprecationInspection.class);
myFixture.configureByFiles("deprecation/usingDeprecatedMethod.py", "deprecation/deprecatedLibrary.py", "deprecation/deprecatedLibrary.pyi");
myFixture.checkHighlighting(true, false, false);
}
public void testCustomDeprecatedAnnotation() {
myFixture.enableInspections(PyDeprecationInspection.class);
myFixture.configureByFile("deprecation/customDeprecatedAnnotation.py");