From f791db7500b9bf3fcb545ec706e90f0df7f933b0 Mon Sep 17 00:00:00 2001 From: Ekaterina Tuzova Date: Tue, 11 Mar 2014 13:26:13 +0400 Subject: [PATCH] fixed PY-10342 Instance attribute defined outside init: detect functions called in init --- .../inspections/PyAttributeOutsideInitInspection.java | 2 ++ .../PyAttributeOutsideInitInspection/privateMethod.py | 8 ++++++++ .../inspections/PyAttributeOutsideInitInspectionTest.java | 4 ++++ 3 files changed, 14 insertions(+) create mode 100644 python/testData/inspections/PyAttributeOutsideInitInspection/privateMethod.py diff --git a/python/src/com/jetbrains/python/inspections/PyAttributeOutsideInitInspection.java b/python/src/com/jetbrains/python/inspections/PyAttributeOutsideInitInspection.java index 791a1b52d38b..76d6a40323cf 100644 --- a/python/src/com/jetbrains/python/inspections/PyAttributeOutsideInitInspection.java +++ b/python/src/com/jetbrains/python/inspections/PyAttributeOutsideInitInspection.java @@ -67,6 +67,8 @@ public class PyAttributeOutsideInitInspection extends PyInspection { public void visitPyFunction(PyFunction node) { final PyClass containingClass = node.getContainingClass(); if (containingClass == null) return; + final String name = node.getName(); + if (name != null && name.startsWith("_")) return; if (!isApplicable(containingClass)) { return; } diff --git a/python/testData/inspections/PyAttributeOutsideInitInspection/privateMethod.py b/python/testData/inspections/PyAttributeOutsideInitInspection/privateMethod.py new file mode 100644 index 000000000000..53034260511b --- /dev/null +++ b/python/testData/inspections/PyAttributeOutsideInitInspection/privateMethod.py @@ -0,0 +1,8 @@ + +class MyClass(object): + def __init__(self): + self._resetData() + self._val = [] + + def _resetData(self): + self._val = [] # This should not be receiving a warning. The function gets called in __init__. \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/inspections/PyAttributeOutsideInitInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/PyAttributeOutsideInitInspectionTest.java index 7c8812f44f97..de041b8a550f 100644 --- a/python/testSrc/com/jetbrains/python/inspections/PyAttributeOutsideInitInspectionTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/PyAttributeOutsideInitInspectionTest.java @@ -66,6 +66,10 @@ public class PyAttributeOutsideInitInspectionTest extends PyTestCase { doTest(); } + public void testPrivateMethod() { + doTest(); + } + private void doTest() { myFixture.configureByFile("inspections/PyAttributeOutsideInitInspection/" + getTestName(true) + ".py"); myFixture.enableInspections(PyAttributeOutsideInitInspection.class);