Object (final PsiElement element) { final PyForStatement forStatement = PsiTreeUtil.getParentOfType(element, PyForStatement.class, false); if (forStatement == null){ return null; } final PyForPart forPart = forStatement.getForPart(); return forPart == element || forPart.getStatementList() == element ? forStatement : null; }