PY-19825 Fixed: Tuple sub-assignment typing broken

Find the highest ancestor of tuple, calculate its type and then extract type of its subtuples and their elements
This commit is contained in:
fitermay
2016-07-14 18:58:40 +03:00
committed by Semyon Proshev
parent 5e59e9281d
commit cf5453a5cf
2 changed files with 31 additions and 5 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -176,16 +176,18 @@ public class PyTargetExpressionImpl extends PyBaseElementImpl<PyTargetExpression
}
if (parent instanceof PyTupleExpression) {
PsiElement nextParent = parent.getParent();
while (nextParent instanceof PyParenthesizedExpression) {
while (nextParent instanceof PyParenthesizedExpression || nextParent instanceof PyTupleExpression) {
nextParent = nextParent.getParent();
}
if (nextParent instanceof PyAssignmentStatement) {
final PyAssignmentStatement assignment = (PyAssignmentStatement)nextParent;
final PyExpression value = assignment.getAssignedValue();
if (value != null) {
final PyExpression lhs = assignment.getLeftHandSideExpression();
final PyTupleExpression targetTuple = PsiTreeUtil.findChildOfType(lhs, PyTupleExpression.class, false);
if (value != null && targetTuple != null) {
final PyType assignedType = PyTypeChecker.toNonWeakType(context.getType(value), context);
if (assignedType instanceof PyTupleType) {
final PyType t = PyTypeChecker.getTargetTypeFromTupleAssignment(this, (PyTupleExpression)parent, (PyTupleType)assignedType);
final PyType t = PyTypeChecker.getTargetTypeFromTupleAssignment(this, targetTuple, (PyTupleType)assignedType);
if (t != null) {
return t;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -834,6 +834,30 @@ public class PyTypeTest extends PyTestCase {
"expr = (1, False) * 2");
}
public void testTupleDestructuring() {
doTest("str",
"_, expr = (1, 'val') ");
}
public void testParensTupleDestructuring() {
doTest("str",
"(_, expr) = (1, 'val') ");
}
// PY-19825
public void testSubTupleDestructuring() {
doTest("str",
"(a, (_, expr)) = (1, (2,'val')) ");
}
// PY-19825
public void testSubTupleIndirectDestructuring() {
doTest("str",
"xs = (2,'val')\n" +
"(a, (_, expr)) = (1, xs) ");
}
public void testConstructorUnification() {
doTest("C[int]",
"class C(object):\n" +