PY-24436 Fixed: False positive: "Class has not __init__ method" if A inherits B which inherits another A with __init__

Use type eval context in PyClassHasNoInitInspection while looking for __init__ or __new__ method
This commit is contained in:
Semyon Proshev
2017-08-03 14:54:22 +03:00
parent 2be8631016
commit b4525c6ecf
4 changed files with 35 additions and 4 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -73,10 +73,10 @@ public class PyClassHasNoInitInspection extends PyInspection {
if (!(type instanceof PyClassType)) return;
}
final PyFunction init = node.findInitOrNew(true, null);
final PyFunction init = node.findInitOrNew(true, myTypeEvalContext);
if (init == null) {
registerProblem(node.getNameIdentifier(), PyBundle.message("INSP.class.has.no.init"),
new AddMethodQuickFix("__init__", node.getName(), false));
new AddMethodQuickFix(PyNames.INIT, node.getName(), false));
}
}
}
@@ -0,0 +1,10 @@
from lib import Alice
class Intermediary(Alice):
pass
class Alice(Intermediary):
def __str__(self):
return 'New Alice'
@@ -0,0 +1,6 @@
class Alice:
def __init__(self):
pass
def __str__(self):
return 'Old Alice'
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package com.jetbrains.python.inspections;
import com.jetbrains.python.fixtures.PyTestCase;
import com.jetbrains.python.psi.LanguageLevel;
public class PyClassHasNoInitInspectionTest extends PyTestCase {
@@ -51,9 +52,23 @@ public class PyClassHasNoInitInspectionTest extends PyTestCase {
doTest();
}
// PY-24436
public void testAInheritsBAndBInheritsImportedAWithDunderInit() {
runWithLanguageLevel(LanguageLevel.PYTHON30, this::doMultiFileTest);
}
private void doTest() {
myFixture.configureByFile("inspections/PyClassHasNoInitInspection/" + getTestName(true) + ".py");
myFixture.enableInspections(PyClassHasNoInitInspection.class);
myFixture.checkHighlighting(false, false, true);
}
private void doMultiFileTest() {
final String folderPath = "inspections/PyClassHasNoInitInspection/" + getTestName(false) + "/";
myFixture.copyDirectoryToProject(folderPath, "");
myFixture.configureFromTempProjectFile("a.py");
myFixture.enableInspections(PyClassHasNoInitInspection.class);
myFixture.checkHighlighting(false, false, true);
}
}