Fix doing type assertions for <ref> is <ref> statements, None should be presented on one side only (PY-29748)

This commit is contained in:
Semyon Proshev
2018-07-02 20:00:19 +03:00
parent 26791bb70a
commit 4ba52eb062
2 changed files with 18 additions and 12 deletions
@@ -103,20 +103,18 @@ public class PyTypeAssertionEvaluator extends PyRecursiveElementVisitor {
final boolean leftIsNone = lhs instanceof PyNoneLiteralExpression || PyNames.NONE.equals(lhs.getName());
final boolean rightIsNone = rhs instanceof PyNoneLiteralExpression || PyNames.NONE.equals(rhs.getName());
if (leftIsNone && rightIsNone) {
return;
}
if (leftIsNone ^ rightIsNone) {
final PyReferenceExpression target = (PyReferenceExpression)(rightIsNone ? lhs : rhs);
final PyReferenceExpression target = (PyReferenceExpression)(rightIsNone ? lhs : rhs);
if (node.isOperator(PyNames.IS)) {
pushAssertion(target, myPositive, false, context -> PyNoneType.INSTANCE);
return;
}
if (node.isOperator(PyNames.IS)) {
pushAssertion(target, myPositive, false, context -> PyNoneType.INSTANCE);
return;
}
if (node.isOperator("isnot")) {
pushAssertion(target, !myPositive, false, context -> PyNoneType.INSTANCE);
return;
if (node.isOperator("isnot")) {
pushAssertion(target, !myPositive, false, context -> PyNoneType.INSTANCE);
return;
}
}
}
@@ -3244,6 +3244,14 @@ public class PyTypeTest extends PyTestCase {
"expr = T");
}
// PY-29748
public void testAfterIdentityComparison() {
doTest("int",
"a = 1\n" +
"if a is a:\n" +
" expr = a");
}
private static List<TypeEvalContext> getTypeEvalContexts(@NotNull PyExpression element) {
return ImmutableList.of(TypeEvalContext.codeAnalysis(element.getProject(), element.getContainingFile()).withTracing(),
TypeEvalContext.userInitiated(element.getProject(), element.getContainingFile()).withTracing());