From 2712a7c43e76035105a3f2adfd0977e58066ca0e Mon Sep 17 00:00:00 2001 From: Shantanu Jain Date: Wed, 4 Sep 2024 17:33:56 -0700 Subject: [PATCH] deduplicate lines with different type var ids Fixes #126 --- mypy_primer/model.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/mypy_primer/model.py b/mypy_primer/model.py index 89f8ea3..1c8c075 100644 --- a/mypy_primer/model.py +++ b/mypy_primer/model.py @@ -355,22 +355,28 @@ def _get_diff(self) -> str: old_lines = old_output.splitlines() new_lines = new_output.splitlines() + def canonicalise(line: str) -> str: + # Replace type variable IDs, see https://github.com/hauntsaninja/mypy_primer/issues/126 + return re.sub(r"`\d+", "", line[2:]) + # mypy's output appears to be nondeterministic for some same line errors, e.g. on pypa/pip # Work around that by ignoring identical removal and addition pairs, e.g. # "- a.py:1: error xyz" and "+ a.py:1: error xyz" diff_lines = [line for line in d.compare(old_lines, new_lines) if line[0] in ("+", "-")] net_change: dict[str, int] = defaultdict(int) for line in diff_lines: - net_change[line[2:]] += 1 if line[0] == "+" else -1 + cline = canonicalise(line) + net_change[cline] += 1 if line[0] == "+" else -1 output_lines: list[str] = [] for line in diff_lines: - if line[0] == "+" and net_change[line[2:]] > 0: + cline = canonicalise(line) + if line[0] == "+" and net_change[cline] > 0: output_lines.append(line) - net_change[line[2:]] -= 1 - elif line[0] == "-" and net_change[line[2:]] < 0: + net_change[cline] -= 1 + elif line[0] == "-" and net_change[cline] < 0: output_lines.append(line) - net_change[line[2:]] += 1 + net_change[cline] += 1 return "\n".join(output_lines)