@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement expression = descriptor.getPsiElement(); if (expression instanceof PyTupleExpression) { PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project); PyExpressionStatement statement = elementGenerator.createFromText( LanguageLevel.forElement(expression), PyExpressionStatement.class, "(" + expression.getText() + ")"); expression.replace(statement.getExpression()); } }
@Nullable protected PsiElement replaceExpression( PsiElement expression, PyExpression newExpression, IntroduceOperation operation) { PyExpressionStatement statement = PsiTreeUtil.getParentOfType(expression, PyExpressionStatement.class); if (statement != null) { if (statement.getExpression() == expression && expression.getUserData(PyReplaceExpressionUtil.SELECTION_BREAKS_AST_NODE) == null) { statement.delete(); return null; } } return PyReplaceExpressionUtil.replaceExpression(expression, newExpression); }
@Nullable public static String extractDeprecationMessage(List<PyStatement> statements) { for (PyStatement statement : statements) { if (statement instanceof PyExpressionStatement) { PyExpressionStatement expressionStatement = (PyExpressionStatement) statement; if (expressionStatement.getExpression() instanceof PyCallExpression) { PyCallExpression callExpression = (PyCallExpression) expressionStatement.getExpression(); if (callExpression.isCalleeText(PyNames.WARN)) { PyReferenceExpression warningClass = callExpression.getArgument(1, PyReferenceExpression.class); if (warningClass != null && (PyNames.DEPRECATION_WARNING.equals(warningClass.getReferencedName()) || PyNames.PENDING_DEPRECATION_WARNING.equals( warningClass.getReferencedName()))) { return PyPsiUtils.strValue(callExpression.getArguments()[0]); } } } } } return null; }