void (@NotNull PyFunction newFunction, UsageInfo @NotNull [] usages) { final Set<PsiFile> usageFiles = new HashSet<>(); for (UsageInfo usage : usages) { usageFiles.add(usage.getFile()); } for (PsiFile file : usageFiles) { if (file != newFunction.getContainingFile()) { PyPsiRefactoringUtil.insertImport(file, newFunction, null, true); } } // References inside the body of function if (newFunction.getContainingFile() != mySourceFile) { for (PsiElement read : myExternalReads) { if (read instanceof PsiNamedElement && read.isValid()) { PyPsiRefactoringUtil.insertImport(newFunction, (PsiNamedElement)read, null, true); } } PyClassRefactoringUtil.optimizeImports(mySourceFile); } }