diff --git a/python/src/com/jetbrains/python/codeInsight/intentions/SpecifyTypeInPy3AnnotationsIntention.java b/python/src/com/jetbrains/python/codeInsight/intentions/SpecifyTypeInPy3AnnotationsIntention.java index 00976042431b..cdb93416f71e 100644 --- a/python/src/com/jetbrains/python/codeInsight/intentions/SpecifyTypeInPy3AnnotationsIntention.java +++ b/python/src/com/jetbrains/python/codeInsight/intentions/SpecifyTypeInPy3AnnotationsIntention.java @@ -71,16 +71,17 @@ public class SpecifyTypeInPy3AnnotationsIntention extends TypeIntention { parameter = (PyParameter)parameter.replace(namedParameter); parameter = CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(parameter); editor.getCaretModel().moveToOffset(parameter.getTextOffset()); - PyAnnotation annotation = namedParameter.getAnnotation(); - assert annotation != null; - PyExpression annotationValue = annotation.getValue(); + PyAnnotation annotation = parameter instanceof PyNamedParameter? ((PyNamedParameter)parameter).getAnnotation() : null; + if (annotation != null) { + PyExpression annotationValue = annotation.getValue(); - final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(parameter); - int replacementStart = annotation.getStartOffsetInParent() + annotationValue.getStartOffsetInParent(); - builder.replaceRange(TextRange.create(replacementStart, - replacementStart + annotationValue.getTextLength()), PyNames.OBJECT); - Template template = ((TemplateBuilderImpl)builder).buildInlineTemplate(); - TemplateManager.getInstance(project).startTemplate(editor, template); + final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(parameter); + int replacementStart = annotation.getStartOffsetInParent() + annotationValue.getStartOffsetInParent(); + builder.replaceRange(TextRange.create(replacementStart, + replacementStart + annotationValue.getTextLength()), PyNames.OBJECT); + Template template = ((TemplateBuilderImpl)builder).buildInlineTemplate(); + TemplateManager.getInstance(project).startTemplate(editor, template); + } } private void annotateReturnType(Project project, PsiElement resolved) { diff --git a/python/src/com/jetbrains/python/psi/impl/PyElementGeneratorImpl.java b/python/src/com/jetbrains/python/psi/impl/PyElementGeneratorImpl.java index eec1e18cbde4..d7249521648b 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyElementGeneratorImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyElementGeneratorImpl.java @@ -254,7 +254,7 @@ public class PyElementGeneratorImpl extends PyElementGenerator { @NotNull LanguageLevel languageLevel) { String parameterText = name; if (annotation != null) - parameterText += " : " + annotation; + parameterText += ": " + annotation; if (defaultValue != null) parameterText += " = " + defaultValue; diff --git a/python/testData/intentions/afterTypeAnnotation3.py b/python/testData/intentions/afterTypeAnnotation3.py index d0c49d419746..054f2a6a231f 100644 --- a/python/testData/intentions/afterTypeAnnotation3.py +++ b/python/testData/intentions/afterTypeAnnotation3.py @@ -1,2 +1,2 @@ -def g(x : object=None ): +def g(x: object=None): return x diff --git a/python/testData/intentions/afterTypeInPy3Annotation.py b/python/testData/intentions/afterTypeInPy3Annotation.py index 3dca05f293e4..53a85344911b 100644 --- a/python/testData/intentions/afterTypeInPy3Annotation.py +++ b/python/testData/intentions/afterTypeInPy3Annotation.py @@ -1,3 +1,3 @@ -def foo(a : object, b): +def foo(a: object, b): a. b = 1 \ No newline at end of file