PyExpression (@NotNull PsiReference reference) { final PsiElement element = reference.getElement(); if (element instanceof PyQualifiedExpression) { final PyExpression qualifier = ((PyQualifiedExpression)element).getQualifier(); if (qualifier != null) { return qualifier; } } if (reference instanceof PyFromImportNameReference) { final PyFromImportStatement statement = PsiTreeUtil.getParentOfType(element, PyFromImportStatement.class); if (statement != null) { final PyReferenceExpression source = statement.getImportSource(); if (source != null) { return source; } } } return null; }