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

deduplicate lines with different type var ids #127

Merged
merged 1 commit into from
Sep 5, 2024
Merged
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
16 changes: 11 additions & 5 deletions mypy_primer/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Loading