PyExceptPart (final PsiElement element) { final PyExceptPart pyExceptPart = PsiTreeUtil.getParentOfType(element, PyExceptPart.class, false); if (pyExceptPart == null){ return null; } final PyExpression expr = pyExceptPart.getTarget(); if (expr != null && PsiTreeUtil.isAncestor(expr, element, false)){ return pyExceptPart; } return null; }