PyForStatement (final PsiElement element) { final PyForStatement forStatement = PsiTreeUtil.getParentOfType(element, PyForStatement.class, false); if (forStatement == null){ return null; } final PyExpression target = forStatement.getForPart().getTarget(); if (target != null && PsiTreeUtil.isAncestor(target, element, false)){ return forStatement; } return null; }