PY-23552 Fixed: PyRedeclaration does not recognize static/class methods

Ignore only functions with unknown or redeclaration decorators in PyRedeclarationInspection
This commit is contained in:
Semyon Proshev
2017-08-01 12:42:30 +03:00
parent 94164f5f97
commit 405a2936d8
7 changed files with 56 additions and 1 deletions
@@ -71,7 +71,8 @@ public class PyRedeclarationInspection extends PyInspection {
@Override
public void visitPyFunction(final PyFunction node) {
if (!isDecorated(node)) {
if (!PyKnownDecoratorUtil.hasUnknownDecorator(node, myTypeEvalContext) &&
!PyKnownDecoratorUtil.hasRedeclarationDecorator(node, myTypeEvalContext)) {
processElement(node);
}
}
@@ -234,6 +234,10 @@ public class PyKnownDecoratorUtil {
return ContainerUtil.exists(getKnownDecorators(function, context), GENERATOR_BASED_COROUTINE_DECORATORS::contains);
}
public static boolean hasRedeclarationDecorator(@NotNull PyFunction function, @NotNull TypeEvalContext context) {
return getKnownDecorators(function, context).contains(TYPING_OVERLOAD);
}
private static boolean allDecoratorsAreKnown(@NotNull PyDecoratable element, @NotNull List<KnownDecorator> decorators) {
final PyDecoratorList decoratorList = element.getDecoratorList();
return decoratorList == null ? decorators.isEmpty() : decoratorList.getDecorators().length == decorators.size();
@@ -0,0 +1,8 @@
class TestClass:
@classmethod
def foo(cls):
print(0)
@classmethod
def <warning descr="Redeclared 'foo' defined above without usage">foo</warning>(cls):
print(1)
@@ -0,0 +1,7 @@
class TestClass:
def foo(self):
print(0)
@classmethod
def <warning descr="Redeclared 'foo' defined above without usage">foo</warning>(cls):
print(1)
@@ -0,0 +1,8 @@
class TestClass:
@staticmethod
def foo():
print(0)
@staticmethod
def <warning descr="Redeclared 'foo' defined above without usage">foo</warning>():
print(1)
@@ -0,0 +1,7 @@
class TestClass:
def foo(self):
print(0)
@staticmethod
def <warning descr="Redeclared 'foo' defined above without usage">foo</warning>():
print(1)
@@ -104,6 +104,26 @@ public class PyRedeclarationInspectionTest extends PyTestCase {
doTest();
}
// PY-23552
public void testStaticMethodRedeclaresInstanceMethod() {
doTest();
}
// PY-23552
public void testClassMethodRedeclaresInstanceMethod() {
doTest();
}
// PY-23552
public void testStaticMethodRedeclaresAnotherStaticMethod() {
doTest();
}
// PY-23552
public void testClassMethodRedeclaresAnotherClassMethod() {
doTest();
}
private void doTest() {
final String path = "inspections/PyRedeclarationInspection/" + getTestName(true) + ".py";