Don't indent an indented `# isort: assignments` block twice `literal.assignments` kept each line's indentation as part of the variable name, and `process` then indents the sorted block by the action comment's indentation again, so inside a class or function the assignments came out one level too deep. The other sort types strip the name already. Strip the indentation here too.
diff --git a/isort/literal.py b/isort/literal.py index 3be7f7c..57027d2 100644 --- a/isort/literal.py +++ b/isort/literal.py
@@ -20,7 +20,7 @@ continue if " = " not in line: raise AssignmentsFormatMismatch(code) - variable_name, value = line.split(" = ", 1) + variable_name, value = line.lstrip().split(" = ", 1) values[variable_name] = value return "".join(
diff --git a/tests/unit/test_regressions.py b/tests/unit/test_regressions.py index 6a28d46..1d21c9c 100644 --- a/tests/unit/test_regressions.py +++ b/tests/unit/test_regressions.py
@@ -3065,3 +3065,9 @@ ) assert isort.code(code) == expected assert isort.code(expected) == expected + + +def test_isort_assignments_in_an_indented_block_keeps_its_indentation() -> None: + """The sorted assignments were indented a second time, which broke the code.""" + test_input = "class C:\n # isort: assignments\n b = 1\n a = 2\n" + assert isort.code(test_input) == "class C:\n # isort: assignments\n a = 2\n b = 1\n"