PY-38928 Parentheses around unpacking for "for" loop target variables break type inference

GitOrigin-RevId: 9debb720880dd77b47c325e426e2d76c74135192
This commit is contained in:
Petr
2024-03-27 13:36:38 +00:00
committed by intellij-monorepo-bot
parent 0173303e0b
commit 3a8596a0e3
2 changed files with 14 additions and 2 deletions
@@ -301,8 +301,9 @@ public class PyTargetExpressionImpl extends PyBaseElementImpl<PyTargetExpression
if (source != null) {
final PyType sourceType = context.getType(source);
final PyType type = getIterationType(sourceType, source, this, context);
if (type instanceof PyTupleType && target instanceof PyTupleExpression) {
return PyTypeChecker.getTargetTypeFromTupleAssignment(this, (PyTupleExpression)target, (PyTupleType)type);
target = PyPsiUtils.flattenParens(target);
if (type instanceof PyTupleType tupleType && target instanceof PyTupleExpression tupleExpression) {
return PyTypeChecker.getTargetTypeFromTupleAssignment(this, tupleExpression, tupleType);
}
if (target == this && type != null) {
return type;
@@ -1114,6 +1114,17 @@ public class PyTypeTest extends PyTestCase {
"(a, (_, expr)) = (1, xs) ");
}
// PY-38928
public void testIterateListOfTuples() {
doTest(
"str",
"""
for ((_, expr)) in [(1, 'foo')]:
pass
"""
);
}
public void testConstructorUnification() {
doTest("C[int]",
"""