From 34fd40bea0a369bc9c4d40c0ee046b59a040a016 Mon Sep 17 00:00:00 2001 From: Ben Mather Date: Mon, 29 Jan 2024 21:14:50 +0000 Subject: [PATCH] Don't reformat classes if statement order unchanged --- src/ssort/_ssort.py | 7 ++++++- tests/test_ssort.py | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/ssort/_ssort.py b/src/ssort/_ssort.py index ce3282b..83cfb23 100644 --- a/src/ssort/_ssort.py +++ b/src/ssort/_ssort.py @@ -229,7 +229,9 @@ def _key(statement): def _statement_text_sorted_class(statement): - head_text, statements = split_class(statement) + head_text, unsorted_statements = split_class(statement) + + statements = list(unsorted_statements) # Take a snapshot of any hard dependencies between statements so that we can # restore them later. @@ -317,6 +319,9 @@ def _statement_text_sorted_class(statement): sorted_statements, graph=runtime_graph ) + if sorted_statements == unsorted_statements: + return statement.text + return ( head_text + "\n" diff --git a/tests/test_ssort.py b/tests/test_ssort.py index edf562c..87da854 100644 --- a/tests/test_ssort.py +++ b/tests/test_ssort.py @@ -678,3 +678,19 @@ def _notdep(self): ) actual = ssort(original) assert actual == expected + + +def test_single_line_dummy_function(): + original = "def fun(): ...\n" + expected = "def fun(): ...\n" + + actual = ssort(original) + assert actual == expected + + +def test_single_line_dummy_class(): + original = "class Class: ...\n" + expected = "class Class: ...\n" + + actual = ssort(original) + assert actual == expected