IDEA-CR-49176: function inline is disabled for overridden methods (PY-36464)

GitOrigin-RevId: 0f24072270482a4d221a7eab6b3f0d6de19c0e35
This commit is contained in:
Aleksei Kniazev
2019-07-08 18:05:36 +03:00
committed by intellij-monorepo-bot
parent 72e1763f81
commit 8800f4763c
5 changed files with 26 additions and 6 deletions
@@ -661,7 +661,8 @@ refactoring.inline.function.special.method=Cannot inline special methods
refactoring.inline.function.decorator=Cannot inline functions with decorators
refactoring.inline.function.self.referrent=Cannot inline functions that reference themselves
refactoring.inline.function.star=Cannot inline functions with * arguments
refactoring.inline.function.overridden=Cannot inline overridden functions
refactoring.inline.function.overrides.method=Cannot inline methods that override other methods
refactoring.inline.function.is.overridden=Cannot inline overridden methods
refactoring.inline.function.global=Cannot inline functions with global variables
refactoring.inline.function.nonlocal=Cannot inline functions with nonlocals variables
refactoring.inline.function.nested=Cannot inline functions with another function declaration
@@ -17,6 +17,7 @@ import com.jetbrains.python.PythonLanguage
import com.jetbrains.python.codeInsight.controlflow.ControlFlowCache
import com.jetbrains.python.psi.*
import com.jetbrains.python.psi.impl.PyBuiltinCache
import com.jetbrains.python.psi.search.PyOverridingMethodsSearch
import com.jetbrains.python.psi.search.PySuperMethodsSearch
import com.jetbrains.python.psi.types.TypeEvalContext
import com.jetbrains.python.pyi.PyiFile
@@ -41,7 +42,8 @@ class PyInlineFunctionHandler : InlineActionHandler() {
hasDecorators(element) -> "refactoring.inline.function.decorator"
hasReferencesToSelf(element) -> "refactoring.inline.function.self.referrent"
hasStarArgs(element) -> "refactoring.inline.function.star"
isOverride(element, project) -> "refactoring.inline.function.overridden"
overridesMethod(element, project) -> "refactoring.inline.function.overrides.method"
isOverridden(element) -> "refactoring.inline.function.is.overridden"
functionScope.hasGlobals() -> "refactoring.inline.function.global"
functionScope.hasNonLocals() -> "refactoring.inline.function.nonlocal"
hasNestedFunction(element) -> "refactoring.inline.function.nested"
@@ -108,11 +110,15 @@ class PyInlineFunctionHandler : InlineActionHandler() {
private fun hasDecorators(function: PyFunction): Boolean = function.decoratorList?.decorators?.isNotEmpty() == true
private fun isOverride(function: PyFunction, project: Project): Boolean {
private fun overridesMethod(function: PyFunction, project: Project): Boolean {
return function.containingClass != null
&& PySuperMethodsSearch.search(function, TypeEvalContext.codeAnalysis(project, function.containingFile)).any()
}
private fun isOverridden(function: PyFunction): Boolean {
return function.containingClass != null && PyOverridingMethodsSearch.search(function, true).any()
}
private fun hasStarArgs(function: PyFunction): Boolean {
return function.parameterList.parameters.asSequence()
.filterIsInstance<PyNamedParameter>()
@@ -8,5 +8,5 @@ class B(A):
print(2)
b = B()
b.meth<caret>od()
a = A()
a.meth<caret>od()
@@ -0,0 +1,12 @@
class A():
def method(self):
print(1)
class B(A):
def method(self):
print(2)
b = B()
b.meth<caret>od()
@@ -94,7 +94,8 @@ class PyInlineFunctionTest : PyTestCase() {
fun testDecorator() = doTestError("Cannot inline functions with decorators")
fun testRecursive() = doTestError("Cannot inline functions that reference themselves")
fun testStar() = doTestError("Cannot inline functions with * arguments")
fun testOverridden() = doTestError("Cannot inline overridden functions")
fun testOverrides() = doTestError("Cannot inline methods that override other methods")
fun testOverridden() = doTestError("Cannot inline overridden methods")
fun testNested() = doTestError("Cannot inline functions with another function declaration")
fun testInterruptedFlow() = doTestError("Cannot inline functions that interrupt control flow")
fun testUsedAsDecorator() = doTestError("Function foo is used as a decorator and cannot be inlined. Function definition will not be removed", isReferenceError = true)