PsiElement (final String name) { final List<RatedResolveResult> results = multiResolveName(name); final List<PsiElement> elements = new ArrayList<>(); for (RatedResolveResult result : results) { final PsiElement element = result.getElement(); final ImportedResolveResult importedResult = PyUtil.as(result, ImportedResolveResult.class); if (importedResult != null) { final PyImportedNameDefiner definer = importedResult.getDefiner(); if (definer != null) { elements.add(definer); } } else if (element != null && element.getContainingFile() == this) { elements.add(element); } } final PsiElement element = elements.isEmpty() ? null : elements.get(elements.size() - 1); if (element != null && !element.isValid()) { throw new PsiInvalidElementAccessException(element); } return element; }