@Override
  protected PsiElement addDeclaration(@NotNull PsiElement expression,
                                      @NotNull PsiElement declaration,
                                      @NotNull IntroduceOperation operation) {
    PsiElement containingFile = expression.getContainingFile();
    assert containingFile instanceof PyFile;
    PsiElement initialPosition = AddImportHelper.getFileInsertPosition((PyFile)containingFile);

    List<PsiElement> sameFileRefs = collectReferencedDefinitionsInSameFile(operation.getElement(), operation.getFile());
    PsiElement maxPosition = getLowermostTopLevelStatement(sameFileRefs);

    if (maxPosition == null) {
      return containingFile.addBefore(declaration, initialPosition);
    }

    assert PyUtil.isTopLevel(maxPosition);
    return containingFile.addAfter(declaration, maxPosition);
  }
@Override
  protected PsiElement addDeclaration(@NotNull PsiElement expression,
                                      @NotNull PsiElement declaration,
                                      @NotNull IntroduceOperation operation)