SearchScope () { if (isQualified()) { return super.getUseScope(); } final ScopeOwner owner = ScopeUtil.getScopeOwner(this); if (owner != null) { final Scope scope = ControlFlowCache.getScope(owner); if (scope.isGlobal(getName())) { return GlobalSearchScope.projectScope(getProject()); } if (scope.isNonlocal(getName())) { return new LocalSearchScope(getContainingFile()); } } // find highest level function containing our var PyElement container = this; while (true) { PyElement parentContainer = PsiTreeUtil.getParentOfType(container, PyFunction.class, PyClass.class); if (parentContainer instanceof PyClass) { if (isQualified()) { return super.getUseScope(); } break; } if (parentContainer == null) { break; } container = parentContainer; } if (container instanceof PyFunction) { return new LocalSearchScope(container); } return super.getUseScope(); }