List<PsiElement> (PsiElement start, boolean deep) { PsiElement seeker = start; List<PsiElement> ret = new ArrayList<>(2); while (seeker != null) { PyFunction func = PsiTreeUtil.getParentOfType(seeker, PyFunction.class, true, PyClass.class); if (func != null) { PyClass cls = func.getContainingClass(); if (cls != null) { ret.add(func); ret.add(cls); return ret; } else if (deep) { ret.add(func); seeker = func; } else { return null; // no immediate class } } else { return null; // no function } } return null; }