From 20f9834816af411f19f50fd29c815aece743c9a1 Mon Sep 17 00:00:00 2001 From: Alec Barber Date: Fri, 12 Apr 2024 15:03:00 +0100 Subject: [PATCH] feat: add support for sorting baseline output --- mypy_baseline/_config.py | 5 +++++ mypy_baseline/commands/_sync.py | 11 +++++++++-- tests/test_commands/test_sync.py | 16 ++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/mypy_baseline/_config.py b/mypy_baseline/_config.py index 8da227c..9800d6b 100644 --- a/mypy_baseline/_config.py +++ b/mypy_baseline/_config.py @@ -29,6 +29,7 @@ class Config: depth: int = 40 allow_unsynced: bool = False preserve_position: bool = False + sort_baseline: bool = False hide_stats: bool = False no_colors: bool = bool(os.environ.get('NO_COLOR')) ignore: list[str] = dataclasses.field(default_factory=list) @@ -68,6 +69,10 @@ def init_parser(self, parser: ArgumentParser) -> None: '--preserve-position', action='store_true', help='do not remove line number from the baseline.', ) + add( + '--sort-baseline', action='store_true', + help='sort the baseline file.', + ) add( '--hide-stats', action='store_true', help='do not show stats at the end.', diff --git a/mypy_baseline/commands/_sync.py b/mypy_baseline/commands/_sync.py index 50e911a..b2ee421 100644 --- a/mypy_baseline/commands/_sync.py +++ b/mypy_baseline/commands/_sync.py @@ -28,6 +28,9 @@ def run(self) -> int: clean_line = error.get_clean_line(self.config) new_baseline.append(clean_line) + if self.config.sort_baseline: + new_baseline.sort() + synced = False if old_baseline: synced = self._stable_sync(old_baseline, new_baseline) @@ -44,9 +47,13 @@ def _stable_sync(self, old_bline: list[str], new_bline: list[str]) -> bool: Currently, we can do a stable sync only when there are no new lines added. It's hard to insert new lines in the correct positions, and adding them at the end of the file will cause merge conflicts. - Sorting lines alphabetically would solve the issue, but I want to keep - backward compatibility. + Sorting lines solves the issue, so we don't use stable sync when the output + is sorted. + However, sorting is not enabled by default because I want to keep backward + compatibility. """ + if not self.config.sort_baseline: + return False old_set = set(old_bline) new_set = set(new_bline) removed = old_set - new_set diff --git a/tests/test_commands/test_sync.py b/tests/test_commands/test_sync.py index d06a35f..82d9940 100644 --- a/tests/test_commands/test_sync.py +++ b/tests/test_commands/test_sync.py @@ -34,3 +34,19 @@ def test_sync_notebook(tmp_path: Path): actual = blpath.read_text() line1 = actual.splitlines()[0] assert line1 == 'fail.ipynb:cell_1:0: error: Incompatible return value type (got "int", expected "str") [return-value]' # noqa: E501 + + +def test_sync_sorted(tmp_path: Path): + blpath = tmp_path / 'bline.txt' + stdin = StringIO() + stdin.write(LINE1) + stdin.write(LINE2) + stdin.write(LINE3) + stdin.seek(0) + code = main(['sync', '--sort-baseline', '--baseline-path', str(blpath)], stdin, StringIO()) # noqa: E501 + assert code == 0 + actual = blpath.read_text() + line1, line2, line3 = actual.splitlines() + assert line1 == 'python/utils.py:0: error: Second argument of Enum() must be string [misc]' # noqa: E501s + assert line2 == 'settings.py:0: error: How are you? [union-attr]' + assert line3 == 'views.py:0: error: Hello world [assignment]'