Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,16 @@ private static void write(@NotNull PyClass pyClass, @NotNull List<PyMethodMember
final PyStatementList statementList = cls.getStatementList();
final TypeEvalContext context = TypeEvalContext.userInitiated(cls.getProject(), cls.getContainingFile());

final PyFunction function = buildOverriddenFunction(cls, baseFunction, implement).addFunctionAfter(statementList, anchor);
final PyFunction function;
final PyFunction newFunction = buildOverriddenFunction(cls, baseFunction, implement).buildFunction();
if (anchor != null) {
function = (PyFunction)statementList.addAfter(newFunction, anchor);
}
else {
// When the statement list is still on the same line as the colon (e.g. the class body is not created yet),
// inserting via plain PSI operations may lead to incorrect indentation (especially for nested classes).
function = (PyFunction)PyPsiRefactoringUtil.addElementToStatementList(newFunction, statementList, true);
}
PyClassRefactoringUtil.transplantImportsFromSignature(baseFunction, function);

PyiUtil
Expand Down
11 changes: 11 additions & 0 deletions python/testData/inspections/ImplementAbstractNestedIndentation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import abc


class Abstract(abc.ABC):
@abc.abstractmethod
async def run(self):
pass


def test_class():
class Conc<caret>rete(Abstract):<EOLError descr="Indent expected"></EOLError>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import abc


class Abstract(abc.ABC):
@abc.abstractmethod
async def run(self):
pass


def test_class():
class Concrete(Abstract):
async def run(self):
pass
4 changes: 4 additions & 0 deletions python/testData/inspections/ImplementAbstractOrder.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def foo0(self):
def foo1(self):
pass

@abstractmethod
def foo2(self):
pass

def bar(self):
pass

Expand Down
6 changes: 6 additions & 0 deletions python/testData/inspections/ImplementAbstractOrder_after.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def foo0(self):
def foo1(self):
pass

@abstractmethod
def foo2(self):
pass

def bar(self):
pass

Expand All @@ -23,3 +27,5 @@ def foo0(self):
def foo1(self):
pass

def foo2(self):
pass
12 changes: 12 additions & 0 deletions python/testSrc/com/jetbrains/python/PyQuickFixTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,18 @@ class PyQuickFixTest : PyTestCase() {
)
}

fun testImplementAbstractNestedIndentation() {
runWithLanguageLevel(LanguageLevel.PYTHON37) {
doInspectionTest(
"ImplementAbstractNestedIndentation.py",
PyAbstractClassInspection::class.java,
PyBundle.message("QFIX.NAME.implement.methods"),
true,
true
)
}
}

fun testRemovingUnderscoresInNumericLiterals() {
myFixture.configureByText(PythonFileType.INSTANCE, "1_0_0")

Expand Down