mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
validate 'assignment to keyword' in Python 3 (PY-1524)
This commit is contained in:
@@ -37,6 +37,7 @@ public class PyNames {
|
||||
|
||||
@NonNls public static final String ALL = "__all__";
|
||||
@NonNls public static final String SLOTS = "__slots__";
|
||||
@NonNls public static final String DEBUG = "__debug__";
|
||||
|
||||
@NonNls private static final Set<String> _UnderscoredAttributes = new HashSet<String>();
|
||||
static {
|
||||
|
||||
@@ -237,4 +237,12 @@ public class PyElementVisitor extends PsiElementVisitor {
|
||||
public void visitPyAssertStatement(final PyAssertStatement node) {
|
||||
visitPyElement(node);
|
||||
}
|
||||
|
||||
public void visitPyNoneLiteralExpression(final PyNoneLiteralExpression node) {
|
||||
visitPyElement(node);
|
||||
}
|
||||
|
||||
public void visitPyBoolLiteralExpression(final PyBoolLiteralExpression node) {
|
||||
visitPyElement(node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.jetbrains.python.psi.impl;
|
||||
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.jetbrains.python.psi.PyBoolLiteralExpression;
|
||||
import com.jetbrains.python.psi.PyElementVisitor;
|
||||
import com.jetbrains.python.psi.types.PyType;
|
||||
import com.jetbrains.python.psi.types.TypeEvalContext;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -22,4 +23,9 @@ public class PyBoolLiteralExpressionImpl extends PyElementImpl implements PyBool
|
||||
public boolean getValue() {
|
||||
return "True".equals(getText());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void acceptPyVisitor(PyElementVisitor pyVisitor) {
|
||||
pyVisitor.visitPyBoolLiteralExpression(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.jetbrains.python.psi.impl;
|
||||
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.jetbrains.python.psi.PyElementVisitor;
|
||||
import com.jetbrains.python.psi.PyNoneLiteralExpression;
|
||||
import com.jetbrains.python.psi.types.PyNoneType;
|
||||
import com.jetbrains.python.psi.types.PyType;
|
||||
@@ -18,4 +19,9 @@ public class PyNoneLiteralExpressionImpl extends PyElementImpl implements PyNone
|
||||
public PyType getType(@NotNull TypeEvalContext context) {
|
||||
return PyNoneType.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void acceptPyVisitor(PyElementVisitor pyVisitor) {
|
||||
pyVisitor.visitPyNoneLiteralExpression(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ public class AssignTargetAnnotator extends PyAnnotator {
|
||||
@Override
|
||||
public void visitPyReferenceExpression(final PyReferenceExpression node) {
|
||||
String referencedName = node.getReferencedName();
|
||||
if (referencedName != null && referencedName.equals(PyNames.NONE)) {
|
||||
if (PyNames.NONE.equals(referencedName)) {
|
||||
getHolder().createErrorAnnotation(node, (_op == Operation.Delete) ? DELETING_NONE : ASSIGNMENT_TO_NONE);
|
||||
}
|
||||
}
|
||||
@@ -74,12 +74,20 @@ public class AssignTargetAnnotator extends PyAnnotator {
|
||||
@Override
|
||||
public void visitPyTargetExpression(final PyTargetExpression node) {
|
||||
String targetName = node.getName();
|
||||
if (targetName != null && targetName.equals(PyNames.NONE)) {
|
||||
if (PyNames.NONE.equals(targetName)) {
|
||||
final VirtualFile vfile = node.getContainingFile().getVirtualFile();
|
||||
if (vfile != null && !vfile.getUrl().contains("/" + PythonSdkType.SKELETON_DIR_NAME + "/")){
|
||||
getHolder().createErrorAnnotation(node, (_op == Operation.Delete) ? DELETING_NONE : ASSIGNMENT_TO_NONE);
|
||||
}
|
||||
}
|
||||
if (PyNames.DEBUG.equals(targetName)) {
|
||||
if (LanguageLevel.forElement(node).isPy3K()) {
|
||||
getHolder().createErrorAnnotation(node, "assignment to keyword");
|
||||
}
|
||||
else {
|
||||
getHolder().createErrorAnnotation(node, "cannot assign to __debug__");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -182,5 +190,15 @@ public class AssignTargetAnnotator extends PyAnnotator {
|
||||
public void visitPyLambdaExpression(final PyLambdaExpression node) {
|
||||
getHolder().createErrorAnnotation(node, message("ANN.cant.assign.to.lambda"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPyNoneLiteralExpression(PyNoneLiteralExpression node) {
|
||||
getHolder().createErrorAnnotation(node, "assignment to keyword");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPyBoolLiteralExpression(PyBoolLiteralExpression node) {
|
||||
getHolder().createErrorAnnotation(node, "assignment to keyword");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,3 +5,7 @@ for a, *b in [(1, 2, 3), (4, 5, 6, 7)]:
|
||||
a, b, c = seq[0], seq[1:-1], seq[-1]
|
||||
a, *b, c = seq
|
||||
[a, *b, c] = seq
|
||||
<error descr="assignment to keyword">True</error> = 2
|
||||
<error descr="assignment to keyword">False</error> = 1
|
||||
<error descr="assignment to keyword">None</error> = 0
|
||||
<error descr="assignment to keyword">__debug__</error> = -1
|
||||
|
||||
Reference in New Issue
Block a user