boolean (@NotNull PsiElement element) { if (element instanceof PsiFileSystemItem) { // may be import via alias, so don't check if names match, do simple resolve check instead PsiElement resolveResult = resolve(); if (resolveResult instanceof PyImportedModule) { resolveResult = resolveResult.getNavigationElement(); } if (element instanceof PsiDirectory) { if (resolveResult instanceof PyFile file) { if (PyUtil.isPackage(file) && file.getContainingDirectory() == element) { return true; } } else if (resolveResult instanceof PsiDirectory directory) { if (PyUtil.isPackage(directory, null) && directory == element) { return true; } } } return resolveResult == element; } if (element instanceof PsiNamedElement) { final String elementName = ((PsiNamedElement)element).getName(); if ((Objects.equals(myElement.getReferencedName(), elementName) || PyNames.INIT.equals(elementName))) { if (!haveQualifiers(element)) { final ScopeOwner ourScopeOwner = ScopeUtil.getScopeOwner(getElement()); final ScopeOwner theirScopeOwner = ScopeUtil.getScopeOwner(element); if (element instanceof PyParameter || element instanceof PyTargetExpression) { // Check if the reference is in the same or inner scope of the element scope, not shadowed by an intermediate declaration if (resolvesToSameLocal(element, elementName, ourScopeOwner, theirScopeOwner)) { return true; } } final List<PsiElement> resolveResults = StreamEx.of(multiResolve(false)) .filter(r -> !(r instanceof ImplicitResolveResult)) .map(r -> r.getElement()) .toList(); for (PsiElement resolveResult : resolveResults) { if (resolveResult == element) { return true; } if (!haveQualifiers(element) && ourScopeOwner != null && theirScopeOwner != null) { if (resolvesToSameGlobal(element, elementName, ourScopeOwner, theirScopeOwner, resolveResult)) return true; } if (resolvesToWrapper(element, resolveResult)) { return true; } } } if (element instanceof PyExpression expr) { if (PyUtil.isClassAttribute(myElement) && (PyUtil.isClassAttribute(expr) || PyUtil.isInstanceAttribute(expr))) { final PyClass c1 = PsiTreeUtil.getParentOfType(element, PyClass.class); final PyClass c2 = PsiTreeUtil.getParentOfType(myElement, PyClass.class); final TypeEvalContext context = myContext.getTypeEvalContext(); if (c1 != null && c2 != null && (c1.isSubclass(c2, context) || c2.isSubclass(c1, context))) { return true; } } } } } return PyReferenceCustomTargetChecker.Companion.isReferenceTo(this, element); }