PY-20832 Fixed: __iter__ is not inferred from for-statement

Update PyNamedParameterImpl.collectUsedAttributes to honour for-statements
This commit is contained in:
Semyon Proshev
2016-10-24 17:39:30 +03:00
parent f7c6927a06
commit e5d97680e6
3 changed files with 30 additions and 10 deletions
@@ -361,6 +361,18 @@ public class PyNamedParameterImpl extends PyBaseElementImpl<PyNamedParameterStub
super.visitPyCallExpression(node);
}
@Override
public void visitPyForStatement(PyForStatement node) {
Optional
.of(node.getForPart())
.map(PyForPart::getSource)
.map(PyExpression::getReference)
.filter(reference -> reference.isReferenceTo(PyNamedParameterImpl.this))
.ifPresent(reference -> result.add(PyNames.ITER));
super.visitPyForStatement(node);
}
});
}
return result;
@@ -19,16 +19,15 @@ def expand(values1, values2):
<weak_warning descr="Assignment can be replaced with augmented assignment">values1 = values1 + values2</weak_warning>
#def expand(values1, values2):
# for a in values1:
# print(a)
#
# for b in values2:
# print(b)
#
# values1 = values2 + values1
# values1 = values1 + values2
# inspection should suggest replacement only for the second assignment
def expand(values1, values2):
for a in values1:
print(a)
for b in values2:
print(b)
values1 = values2 + values1
<weak_warning descr="Assignment can be replaced with augmented assignment">values1 = values1 + values2</weak_warning>
def expand(values1, values2):
@@ -1145,6 +1145,15 @@ public class PyTypeTest extends PyTestCase {
"expr = f\n");
}
// PY-20832
public void testStructuralTypeWithDunderIter() {
doTest("{__iter__}",
"def expand(values1):\n" +
" for a in values1:\n" +
" print(a)\n" +
" expr = values1\n");
}
// PY-20833
public void testStructuralTypeWithDunderLen() {
doTest("{__len__}",