@NotNull
  private static List<PsiElement> doResolveQualifiedNameInScope(@NotNull QualifiedName qualifiedName,
                                                                @NotNull ScopeOwner scopeOwner,
                                                                @NotNull TypeEvalContext context) {
    final String firstName = qualifiedName.getFirstComponent();
    if (firstName == null || !(scopeOwner instanceof PyTypedElement)) return Collections.emptyList();

    final PyResolveContext resolveContext = PyResolveContext.defaultContext(context);

    final List<? extends RatedResolveResult> unqualifiedResults;
    if (scopeOwner instanceof PyiFile fileScope) {
      // pyi-stubs are special cased because
      // `resolveMember` delegates to `multiResolveName(..., true)` and
      // it skips elements that are imported without `as`
      unqualifiedResults = fileScope.multiResolveName(firstName, false);
    }
    else if (scopeOwner instanceof PyFunction functionScope) {
      final Stream<PsiNamedElement> targets = StreamEx
        .of(PsiTreeUtil.getStubChildrenOfTypeAsList(scopeOwner, PyTargetExpression.class))
        .filter(it -> !it.isQualified())
        .select(PsiNamedElement.class);

      final Stream<PsiNamedElement> parameters = StreamEx
        .of(functionScope.getParameterList().getParameters())
        .select(PsiNamedElement.class);

      unqualifiedResults = StreamEx
        .of(targets)
        .append(parameters)
        .filter(it -> firstName.equals(it.getName()))
        .map(it -> new RatedResolveResult(RatedResolveResult.RATE_NORMAL, it))
        .append(resolveTypeParameters(functionScope, firstName))
        .toList();
    }
    else if (scopeOwner instanceof PyTypeAliasStatement) {
      unqualifiedResults = resolveTypeParameters((PyTypeParameterListOwner)scopeOwner, firstName);
    }
    else {
      final PyType scopeType = context.getType((PyTypedElement)scopeOwner);
      if (scopeType == null) return Collections.emptyList();
      List<? extends RatedResolveResult> typeMembers = scopeType.resolveMember(firstName, null, AccessDirection.READ, resolveContext);
      if (scopeOwner instanceof PyClass pyClass) {
        unqualifiedResults = ContainerUtil.concat(ContainerUtil.notNullize(typeMembers), resolveTypeParameters(pyClass, firstName));
      }
      else {
        unqualifiedResults = typeMembers;
      }
    }

    final StreamEx<RatedResolveResult> initialResults;
    if (ContainerUtil.isEmpty(unqualifiedResults)) {
      final PsiElement builtin = PyBuiltinCache.getInstance(scopeOwner).getByName(firstName);
      if (builtin == null) {
        return Collections.emptyList();
      }
      initialResults = StreamEx.of(new RatedResolveResult(RatedResolveResult.RATE_NORMAL, builtin));
    }
    else {
      initialResults = StreamEx.of(unqualifiedResults);
    }

    final List<String> remainingNames = qualifiedName.removeHead(1).getComponents();
    final StreamEx<RatedResolveResult> result = StreamEx.of(remainingNames).foldLeft(initialResults, (prev, name) ->
      prev
        .map(RatedResolveResult::getElement)
        .select(PyTypedElement.class)
        .map(context::getType)
        .nonNull()
        .flatMap(type -> {
          // An instance type has access to instance attributes defined in __init__, a class type does not.
          final PyType instanceType = type instanceof PyClassLikeType ? ((PyClassLikeType)type).toInstance() : type;
          final List<? extends RatedResolveResult> results = instanceType.resolveMember(name, null, AccessDirection.READ, resolveContext);
          return results != null ? StreamEx.of(results) : StreamEx.<RatedResolveResult>empty();
        }));

    return Collections.unmodifiableList(PyUtil.filterTopPriorityResults(result.toArray(RatedResolveResult[]::new)));
  }
@NotNull
  private static List<PsiElement> doResolveQualifiedNameInScope(@NotNull QualifiedName qualifiedName,
                                                                @NotNull ScopeOwner scopeOwner,
                                                                @NotNull TypeEvalContext context)