void (@NotNull PyTargetExpression target, @NotNull TypeEvalContext context, AnnotationInfo info, boolean startTemplate) { final PyClass pyClass = target.getContainingClass(); if (pyClass == null) { throw new IllegalArgumentException("Target '" + target.getText() + "' in not contained in a class definition"); } if (!FileModificationService.getInstance().preparePsiElementForWrite(target)) return; final PyElementGenerator generator = PyElementGenerator.getInstance(target.getProject()); final LanguageLevel langLevel = LanguageLevel.forElement(target); final String assignedValue = langLevel.isPython2() ? "None" : "..."; final String declarationText = target.getName() + " = " + assignedValue + " " + TYPE_COMMENT_PREFIX + info.getAnnotationText(); final PyAssignmentStatement declaration = generator.createFromText(langLevel, PyAssignmentStatement.class, declarationText); final PsiElement anchorBefore = findPrecedingAnchorForAttributeDeclaration(pyClass); WriteAction.run(() -> { final PyAssignmentStatement inserted = (PyAssignmentStatement)pyClass.getStatementList().addAfter(declaration, anchorBefore); PsiComment insertedComment = as(inserted.getLastChild(), PsiComment.class); if (insertedComment == null) return; addImportsForTypeAnnotations(info.getTypes(), context, target.getContainingFile()); insertedComment = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(insertedComment); if (startTemplate && insertedComment != null) { openEditorAndAddTemplateForTypeComment(insertedComment, info.getAnnotationText(), info.getTypeRanges()); } }); }