QualifiedName (@NotNull Couple<PsiElement> param) { final PsiElement symbol = param.getFirst(); final PsiElement foothold = param.getSecond(); final PsiFileSystemItem srcfile = PyPsiUtils.getFileSystemItem(symbol); if (srcfile == null) { return null; } VirtualFile virtualFile = srcfile.getVirtualFile(); if (virtualFile == null) { return null; } if (srcfile instanceof PsiFile && symbol instanceof PsiNamedElement && !(symbol instanceof PsiFileSystemItem)) { PsiElement toplevel = symbol; if (symbol instanceof PyFunction) { final PyClass containingClass = ((PyFunction)symbol).getContainingClass(); if (containingClass != null) { toplevel = containingClass; } } PsiDirectory dir = ((PsiFile)srcfile).getContainingDirectory(); while (dir != null) { PyFile initPy = as(PyUtil.turnDirIntoInit(dir), PyFile.class); if (initPy == null) { break; } if (initPy.getImportTargets().isEmpty() && initPy.getFromImports().isEmpty()) { initPy = jumpFromBinarySkeletonsToRealInitPy(initPy); } //noinspection ConstantConditions final List<RatedResolveResult> resolved = initPy.multiResolveName(((PsiNamedElement)toplevel).getName()); final PsiElement finalTopLevel = toplevel; if (resolved.stream().anyMatch(r -> r.getElement() == finalTopLevel)) { virtualFile = dir.getVirtualFile(); } dir = dir.getParentDirectory(); } } final QualifiedName qname = findShortestImportableQName(foothold != null ? foothold : symbol, virtualFile); if (qname != null) { final QualifiedName restored = canonizeQualifiedName(symbol, qname, foothold); if (restored != null) return restored; } return qname; }