diff --git a/python/src/com/jetbrains/python/PyBundle.properties b/python/src/com/jetbrains/python/PyBundle.properties index f34d8aa1039b..db738e9ef448 100644 --- a/python/src/com/jetbrains/python/PyBundle.properties +++ b/python/src/com/jetbrains/python/PyBundle.properties @@ -657,6 +657,7 @@ refactoring.inline.function.generator=Cannot inline generators refactoring.inline.function.async=Cannot inline async functions refactoring.inline.function.constructor=Cannot inline constructor calls refactoring.inline.function.builtin=Cannot inline builtin functions +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 diff --git a/python/src/com/jetbrains/python/refactoring/inline/PyInlineFunctionHandler.kt b/python/src/com/jetbrains/python/refactoring/inline/PyInlineFunctionHandler.kt index e9851107eec1..2ee32b72d47f 100644 --- a/python/src/com/jetbrains/python/refactoring/inline/PyInlineFunctionHandler.kt +++ b/python/src/com/jetbrains/python/refactoring/inline/PyInlineFunctionHandler.kt @@ -37,6 +37,7 @@ class PyInlineFunctionHandler : InlineActionHandler() { element.isGenerator -> "refactoring.inline.function.generator" PyNames.INIT == element.name -> "refactoring.inline.function.constructor" PyBuiltinCache.getInstance(element).isBuiltin(element) -> "refactoring.inline.function.builtin" + isSpecialMethod(element) -> "refactoring.inline.function.special.method" hasDecorators(element) -> "refactoring.inline.function.decorator" hasReferencesToSelf(element) -> "refactoring.inline.function.self.referrent" hasStarArgs(element) -> "refactoring.inline.function.star" @@ -56,6 +57,10 @@ class PyInlineFunctionHandler : InlineActionHandler() { } } + private fun isSpecialMethod(function: PyFunction): Boolean { + return function.containingClass != null && PyNames.getBuiltinMethods(LanguageLevel.forElement(function)).contains(function.name) + } + private fun hasNestedFunction(function: PyFunction): Boolean = SyntaxTraverser.psiTraverser(function.statementList).traverse().any { it is PyFunction } private fun hasNonExhaustiveIfs(function: PyFunction): Boolean { diff --git a/python/testData/refactoring/inlineFunction/specialMethod.py b/python/testData/refactoring/inlineFunction/specialMethod.py new file mode 100644 index 000000000000..f75b1bf0e736 --- /dev/null +++ b/python/testData/refactoring/inlineFunction/specialMethod.py @@ -0,0 +1,9 @@ +class Additive: + def __init__(self, value): + self.value = value + + def __add__(self, other): + return Additive(self.value + other.value) + + +Additive(1) + Additive(1) \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/refactoring/PyInlineFunctionTest.kt b/python/testSrc/com/jetbrains/python/refactoring/PyInlineFunctionTest.kt index 753c97274a40..4e69869a007e 100644 --- a/python/testSrc/com/jetbrains/python/refactoring/PyInlineFunctionTest.kt +++ b/python/testSrc/com/jetbrains/python/refactoring/PyInlineFunctionTest.kt @@ -90,6 +90,7 @@ class PyInlineFunctionTest : PyTestCase() { } fun testConstructor() = doTestError("Cannot inline constructor calls") fun testBuiltin() = doTestError("Cannot inline builtin functions") + fun testSpecialMethod() = doTestError("Cannot inline special methods") 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")