From 482009a18020164dbeec94a0cdcd1a9de64f9966 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Fri, 15 May 2015 01:44:00 +0300 Subject: [PATCH] PY-11561 Preserve existing type annotations in the modified constructor --- .../inspections/quickfix/AddCallSuperQuickFix.java | 6 +++++- .../inspections/AddCallSuperAnnotations_after.py | 2 +- .../AddCallSuperTypeAnnotationsPreserved.py | 7 +++++++ .../AddCallSuperTypeAnnotationsPreserved_after.py | 7 +++++++ .../testSrc/com/jetbrains/python/PyQuickFixTest.java | 10 ++++++++++ 5 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 python/testData/inspections/AddCallSuperTypeAnnotationsPreserved.py create mode 100644 python/testData/inspections/AddCallSuperTypeAnnotationsPreserved_after.py diff --git a/python/src/com/jetbrains/python/inspections/quickfix/AddCallSuperQuickFix.java b/python/src/com/jetbrains/python/inspections/quickfix/AddCallSuperQuickFix.java index 52087e9fbcb3..193104bdd3bb 100644 --- a/python/src/com/jetbrains/python/inspections/quickfix/AddCallSuperQuickFix.java +++ b/python/src/com/jetbrains/python/inspections/quickfix/AddCallSuperQuickFix.java @@ -91,7 +91,11 @@ public class AddCallSuperQuickFix implements LocalQuickFix { newFunction.append(", "); } StringUtil.join(newParameters, ", ", newFunction); - newFunction.append("):\n\t"); + newFunction.append(")"); + if (problemFunction.getAnnotation() != null) { + newFunction.append(problemFunction.getAnnotation().getText()); + } + newFunction.append(":\n\t"); final List superCallArguments = couple.getSecond(); if (addComma && !superCallArguments.isEmpty()) { diff --git a/python/testData/inspections/AddCallSuperAnnotations_after.py b/python/testData/inspections/AddCallSuperAnnotations_after.py index 4bfd08c375e3..ddee178f3ace 100644 --- a/python/testData/inspections/AddCallSuperAnnotations_after.py +++ b/python/testData/inspections/AddCallSuperAnnotations_after.py @@ -4,5 +4,5 @@ class Example1: class Example2(Example1): - def __init__(self, field1): + def __init__(self, field1: str): super().__init__(field1) \ No newline at end of file diff --git a/python/testData/inspections/AddCallSuperTypeAnnotationsPreserved.py b/python/testData/inspections/AddCallSuperTypeAnnotationsPreserved.py new file mode 100644 index 000000000000..76b33e20063e --- /dev/null +++ b/python/testData/inspections/AddCallSuperTypeAnnotationsPreserved.py @@ -0,0 +1,7 @@ +class A: + def __init__(self, a:int, b:float, *args:tuple, c:complex, **kwargs:dict) -> None: + pass + +class B(A): + def __init__(self, d:str, *, e:bytes) -> list: + pass \ No newline at end of file diff --git a/python/testData/inspections/AddCallSuperTypeAnnotationsPreserved_after.py b/python/testData/inspections/AddCallSuperTypeAnnotationsPreserved_after.py new file mode 100644 index 000000000000..c8b5b3e67c00 --- /dev/null +++ b/python/testData/inspections/AddCallSuperTypeAnnotationsPreserved_after.py @@ -0,0 +1,7 @@ +class A: + def __init__(self, a:int, b:float, *args:tuple, c:complex, **kwargs:dict) -> None: + pass + +class B(A): + def __init__(self, d:str, a:int, b:float, *args:tuple, e:bytes, c:complex, **kwargs:dict) -> list: + super().__init__(a, b, *args, c=c, **kwargs) \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PyQuickFixTest.java b/python/testSrc/com/jetbrains/python/PyQuickFixTest.java index 6ca5fe3e4211..99a0dc27efbb 100644 --- a/python/testSrc/com/jetbrains/python/PyQuickFixTest.java +++ b/python/testSrc/com/jetbrains/python/PyQuickFixTest.java @@ -400,7 +400,17 @@ public class PyQuickFixTest extends PyTestCase { PyBundle.message("QFIX.add.super"), true, true); } }); + } + // PY-15867 + public void testAddCallSuperTypeAnnotationsPreserved() { + runWithLanguageLevel(LanguageLevel.PYTHON30, new Runnable() { + @Override + public void run() { + doInspectionTest("AddCallSuperTypeAnnotationsPreserved.py", PyMissingConstructorInspection.class, + PyBundle.message("QFIX.add.super"), true, true); + } + }); } //PY-491, PY-13297