From 10fffd25a857a4ef3b1bacc83d4dcc09c2852bb7 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Mon, 15 Aug 2016 17:25:13 +0300 Subject: [PATCH] PY-20058 Fix the check that we should suppress E402 in Jupyter notebooks So that it ignores preceding comments and imports and, overall, mutes this error not only on the first import statement in a cell. Also I accidentally used the interface PyImportStatement instead of PyImportStatementBase before, so, the previous fix didn't handle the original case with "from __future__ import print_function". --- .../plugins/ipnb/IpnbPep8ProblemSuppressor.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/python/ipnb/src/org/jetbrains/plugins/ipnb/IpnbPep8ProblemSuppressor.java b/python/ipnb/src/org/jetbrains/plugins/ipnb/IpnbPep8ProblemSuppressor.java index 5e152740ae75..66741696ebb8 100644 --- a/python/ipnb/src/org/jetbrains/plugins/ipnb/IpnbPep8ProblemSuppressor.java +++ b/python/ipnb/src/org/jetbrains/plugins/ipnb/IpnbPep8ProblemSuppressor.java @@ -3,10 +3,7 @@ package org.jetbrains.plugins.ipnb; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.util.PsiTreeUtil; -import com.jetbrains.python.psi.PyEmptyExpression; -import com.jetbrains.python.psi.PyExpressionStatement; -import com.jetbrains.python.psi.PyFile; -import com.jetbrains.python.psi.PyImportStatement; +import com.jetbrains.python.psi.*; import com.jetbrains.python.psi.impl.PyPsiUtils; import com.jetbrains.python.validation.Pep8ExternalAnnotator; import com.jetbrains.python.validation.Pep8ProblemSuppressor; @@ -32,18 +29,18 @@ public class IpnbPep8ProblemSuppressor implements Pep8ProblemSuppressor { // Ignore warnings about imports not at the top of file if there are magic commands before if (problem.getCode().equals("E402") && targetElement != null) { - final PyImportStatement importStatement = PsiTreeUtil.getParentOfType(targetElement, PyImportStatement.class); + final PyImportStatementBase importStatement = PsiTreeUtil.getParentOfType(targetElement, PyImportStatementBase.class); if (importStatement != null && importStatement.getParent() == file) { boolean containsMagicCommandsBefore = false; - PsiElement prev = PyPsiUtils.getPrevNonWhitespaceSibling(importStatement); + PsiElement prev = PyPsiUtils.getPrevNonCommentSibling(importStatement, true); while (prev != null) { if (prev instanceof PyEmptyExpression && prev.getText().startsWith("%")) { containsMagicCommandsBefore = true; } - else if (!isModuleLevelDocstring(prev, (PyFile)file)) { + else if (!(isModuleLevelDocstring(prev, (PyFile)file) || prev instanceof PyImportStatementBase)) { return false; } - prev = PyPsiUtils.getPrevNonWhitespaceSibling(prev); + prev = PyPsiUtils.getPrevNonCommentSibling(prev, true); } return containsMagicCommandsBefore; }