PsiElement (@NotNull PyElement element) { final PsiFile file = element.getContainingFile(); if (!(file instanceof PyiFile)) return null; final PyFile originalFile = getOriginalFile((PyiFile)file); if (originalFile == null) return null; PsiElement result = findSimilarElement(element, originalFile); // If a name is defined in a .pyi stub and the corresponding .py module in a different manner, e.g. // it's exported through an assignment to a top-level attribute in a .pyi stub, but though a regular // "from" import in .py file, we might end up in another stub file again. It happens because resolving // an imported name in a .py file in findSimilarElement() still prioritizes .pyi stubs over implementations // as PyResolveContext doesn't retain the PyQualifiedNameResolveContext.getWithoutStubs flag. // TODO propagate "without stubs" property through PyResolveContext if (result instanceof PyElement && isInsideStub(result) && result.getContainingFile() != file) { result = getOriginalElement((PyElement)result); } if (result != null) return result; if (element instanceof PyFunction) { PyClass containingClass = PyUtil.turnConstructorIntoClass((PyFunction)element); if (containingClass != null) { result = findSimilarElement(containingClass, originalFile); } } return result; }