void (@NotNull PyTargetExpression target, @Nullable TypeEvalContext context, @NotNull AnnotationInfo info, boolean startTemplate) { final LanguageLevel langLevel = LanguageLevel.forElement(target); if (langLevel.isOlderThan(LanguageLevel.PYTHON36)) { throw new IllegalArgumentException("Target '" + target.getText() + "' doesn't belong to Python 3.6+ project: " + langLevel); } if (!FileModificationService.getInstance().preparePsiElementForWrite(target)) return; final Project project = target.getProject(); final ThrowableComputable<PyAnnotationOwner, RuntimeException> addOrUpdateAnnotatedStatement; if (canUseInlineAnnotation(target)) { final SmartPointerManager manager = SmartPointerManager.getInstance(project); final PyAssignmentStatement assignment = (PyAssignmentStatement)target.getParent(); final SmartPsiElementPointer<PyAssignmentStatement> pointer = manager.createSmartPsiElementPointer(assignment); addOrUpdateAnnotatedStatement = () -> { PyUtil.updateDocumentUnblockedAndCommitted(target, document -> { document.insertString(target.getTextRange().getEndOffset(), ": " + info.getAnnotationText()); }); return pointer.getElement(); }; } else { final PyElementGenerator generator = PyElementGenerator.getInstance(project); final String declarationText = target.getName() + ": " + info.getAnnotationText(); final PyTypeDeclarationStatement declaration = generator.createFromText(langLevel, PyTypeDeclarationStatement.class, declarationText); final PyStatement statement = PsiTreeUtil.getParentOfType(target, PyStatement.class); assert statement != null; addOrUpdateAnnotatedStatement = () -> (PyAnnotationOwner)statement.getParent().addBefore(declaration, statement); } WriteAction.run(() -> { PyAnnotationOwner createdAnnotationOwner = addOrUpdateAnnotatedStatement.compute(); if (createdAnnotationOwner == null) return; if (context != null) { addImportsForTypeAnnotations(info.getTypes(), context, target.getContainingFile()); } createdAnnotationOwner = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(createdAnnotationOwner); if (startTemplate && createdAnnotationOwner != null) { openEditorAndAddTemplateForAnnotation(createdAnnotationOwner); } }); }