From 1472b0eef60fe41fd84fff35aee4eb5c20d39950 Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Tue, 9 Apr 2013 17:55:19 +0400 Subject: [PATCH] fixed PY-9302 Class has no __init__ method: false positive for child class without one --- .../python/inspections/PyClassHasNoInitInspection.java | 8 ++++---- .../PyClassHasNoInitInspection/initInParentClass.py | 9 +++++++++ .../inspections/PyClassHasNoInitInspectionTest.java | 4 ++++ 3 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 python/testData/inspections/PyClassHasNoInitInspection/initInParentClass.py diff --git a/python/src/com/jetbrains/python/inspections/PyClassHasNoInitInspection.java b/python/src/com/jetbrains/python/inspections/PyClassHasNoInitInspection.java index 89555fc3ce09..83239fe6e109 100644 --- a/python/src/com/jetbrains/python/inspections/PyClassHasNoInitInspection.java +++ b/python/src/com/jetbrains/python/inspections/PyClassHasNoInitInspection.java @@ -40,14 +40,14 @@ public class PyClassHasNoInitInspection extends PyInspection { @Override public void visitPyClass(PyClass node) { - final PyFunction init = node.findMethodByName(PyNames.INIT, false); - if (init == null) { + final PyFunction init = node.findInitOrNew(true); + if (init == null || PyNames.NEW.equals(init.getName())) { registerProblem(node.getNameIdentifier(), PyBundle.message("INSP.class.has.no.init"), new AddMethodQuickFix("__init__", new PyClassTypeImpl(node, false), false)); } for (PyClass ancestor : node.iterateAncestorClasses()) { - final PyFunction ancestorInit = ancestor.findMethodByName(PyNames.INIT, false); - if (ancestorInit == null) { + final PyFunction ancestorInit = ancestor.findInitOrNew(false); + if (ancestorInit == null || PyNames.NEW.equals(ancestorInit.getName())) { registerProblem(node.getNameIdentifier(), PyBundle.message("INSP.parent.$0.has.no.init", ancestor.getName()), new AddMethodQuickFix("__init__", new PyClassTypeImpl(ancestor, false), false)); } diff --git a/python/testData/inspections/PyClassHasNoInitInspection/initInParentClass.py b/python/testData/inspections/PyClassHasNoInitInspection/initInParentClass.py new file mode 100644 index 000000000000..435a4c29d374 --- /dev/null +++ b/python/testData/inspections/PyClassHasNoInitInspection/initInParentClass.py @@ -0,0 +1,9 @@ +__author__ = 'ktisha' +class Base(object): + def __init__(self): + self.my = 1 + + +class Child(Base): # <- Child class here should not be highlighted since +# it has init mehtod of Base class. + pass diff --git a/python/testSrc/com/jetbrains/python/inspections/PyClassHasNoInitInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/PyClassHasNoInitInspectionTest.java index 266c13ee8d98..000edf8aaaff 100644 --- a/python/testSrc/com/jetbrains/python/inspections/PyClassHasNoInitInspectionTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/PyClassHasNoInitInspectionTest.java @@ -19,6 +19,10 @@ public class PyClassHasNoInitInspectionTest extends PyTestCase { doTest(); } + public void testInitInParentClass() { + doTest(); + } + private void doTest() { myFixture.configureByFile("inspections/PyClassHasNoInitInspection/" + getTestName(true) + ".py"); myFixture.enableInspections(PyClassHasNoInitInspection.class);