PsiElement (@NotNull PyExpression calleeQualifier) { if (calleeQualifier instanceof PyCallExpression) { return Optional .of((PyCallExpression)calleeQualifier) .map(PyCallExpression::getCallee) .map(PyExpression::getReference) .map(PsiReference::resolve) .orElse(null); } else { return Optional .ofNullable(calleeQualifier.getReference()) .map(PsiReference::resolve) .orElse(null); } }