PY-21175 If type assertion excludes a type from Any, leave Any as the result

This commit is contained in:
Mikhail Golubev
2017-07-24 14:38:41 +03:00
parent baf11fece0
commit 58f32e7a70
2 changed files with 21 additions and 1 deletions
@@ -167,7 +167,7 @@ public class PyTypeAssertionEvaluator extends PyRecursiveElementVisitor {
else if (initial instanceof PyUnionType) {
return Ref.create(((PyUnionType)initial).exclude(transformedType, context));
}
else if (PyTypeChecker.match(transformedType, initial, context)) {
else if (initial != null && PyTypeChecker.match(transformedType, initial, context)) {
return null;
}
return Ref.create(initial);
@@ -2012,6 +2012,26 @@ public class PyTypeTest extends PyTestCase {
" expr = var");
}
// PY-21626
public void testNestedConflictingIsNoneChecksInitialAny() {
doTest("Optional[Any]",
"def f(x):\n" +
" if x is None:\n" +
" if x is not None:\n" +
" pass\n" +
" expr = x");
}
// PY-21626
public void testNestedConflictingIsNoneChecksInitialKnown() {
doTest("Optional[str]",
"x = 'foo'\n" +
"if x is None:\n" +
" if x is not None:\n" +
" pass\n" +
"expr = x");
}
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());