summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTamas Szabo <szabtam@gmail.com>2020-10-01 10:56:41 +0300
committerTamas Szabo <szabtam@gmail.com>2020-10-01 10:56:41 +0300
commit28ccbe23c5629a14c43bfb62e5f6c7ce78dfbd0e (patch)
treeb3347c969c1a3df5061ccd0c7542a4d8474b57eb
parent44db9ff4aa342197bebb9c50829a2309862d651b (diff)
downloadisort-issue/1515/isort-assignments-sorting-bug.tar.gz
Fixes bug in isort-assignments sorting.issue/1515/isort-assignments-sorting-bug
Fixes #1515. Bonus: increases branch coverage of literals.py to 100%
-rw-r--r--isort/literal.py21
-rw-r--r--tests/unit/test_literal.py8
-rw-r--r--tests/unit/test_ticketed_features.py8
3 files changed, 21 insertions, 16 deletions
diff --git a/isort/literal.py b/isort/literal.py
index 28e0855c..01bd05e7 100644
--- a/isort/literal.py
+++ b/isort/literal.py
@@ -21,17 +21,18 @@ type_mapping: Dict[str, Tuple[type, Callable[[Any, ISortPrettyPrinter], str]]] =
def assignments(code: str) -> str:
- sort_assignments = {}
+ values = {}
for line in code.splitlines(keepends=True):
- if line:
- if " = " not in line:
- raise AssignmentsFormatMismatch(code)
- else:
- variable_name, value = line.split(" = ", 1)
- sort_assignments[variable_name] = value
-
- sorted_assignments = dict(sorted(sort_assignments.items(), key=lambda item: item[1]))
- return "".join(f"{key} = {value}" for key, value in sorted_assignments.items())
+ if not line.strip():
+ continue
+ if " = " not in line:
+ raise AssignmentsFormatMismatch(code)
+ variable_name, value = line.split(" = ", 1)
+ values[variable_name] = value
+
+ return "".join(
+ f"{variable_name} = {values[variable_name]}" for variable_name in sorted(values.keys())
+ )
def assignment(code: str, sort_type: str, extension: str, config: Config = DEFAULT_CONFIG) -> str:
diff --git a/tests/unit/test_literal.py b/tests/unit/test_literal.py
index 0dd7458c..ee623927 100644
--- a/tests/unit/test_literal.py
+++ b/tests/unit/test_literal.py
@@ -20,7 +20,7 @@ def test_invalid_sort_type():
isort.literal.assignment("x = [1, 2, 3", "tuple-list-not-exist", "py")
-def test_value_assignment():
+def test_value_assignment_list():
assert isort.literal.assignment("x = ['b', 'a']", "list", "py") == "x = ['a', 'b']"
assert (
isort.literal.assignment("x = ['b', 'a']", "list", "py", Config(formatter="example"))
@@ -28,6 +28,10 @@ def test_value_assignment():
)
+def test_value_assignment_assignments():
+ assert isort.literal.assignment("b = 1\na = 2\n", "assignments", "py") == "a = 2\nb = 1\n"
+
+
def test_assignments_invalid_section():
with pytest.raises(exceptions.AssignmentsFormatMismatch):
- isort.literal.assignment("x++", "assignments", "py")
+ isort.literal.assignment("\n\nx = 1\nx++", "assignments", "py")
diff --git a/tests/unit/test_ticketed_features.py b/tests/unit/test_ticketed_features.py
index f952bdc5..51b13009 100644
--- a/tests/unit/test_ticketed_features.py
+++ b/tests/unit/test_ticketed_features.py
@@ -442,9 +442,9 @@ def method():
# isort: assignments
-d = x
+d = 1
b = 2
-a = 1
+a = 3
# isort: dict
y = {"z": "z", "b": "b", "b": "c"}""",
@@ -476,9 +476,9 @@ def method():
# isort: assignments
-a = 1
+a = 3
b = 2
-d = x
+d = 1
# isort: dict
y = {"b": "c", "z": "z"}"""