Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sorting baseline output #22

Merged
merged 1 commit into from
Apr 13, 2024
Merged
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
5 changes: 5 additions & 0 deletions mypy_baseline/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.',
Expand Down
11 changes: 9 additions & 2 deletions mypy_baseline/commands/_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/test_commands/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]'
Loading