MCPcopy Create free account
hub / github.com/python/mypy / show_align_message

Function show_align_message

mypy/test/helpers.py:220–269  ·  view source on GitHub ↗

Align s1 and s2 so that the their first difference is highlighted. For example, if s1 is 'foobar' and s2 is 'fobar', display the following lines: E: foobar A: fobar ^ If s1 and s2 are long, only display a fragment of the strings around the first difference.

(s1: str, s2: str)

Source from the content-addressed store, hash-verified

218
219
220def show_align_message(s1: str, s2: str) -> None:
221 """Align s1 and s2 so that the their first difference is highlighted.
222
223 For example, if s1 is 'foobar' and s2 is 'fobar', display the
224 following lines:
225
226 E: foobar
227 A: fobar
228 ^
229
230 If s1 and s2 are long, only display a fragment of the strings around the
231 first difference. If s1 is very short, do nothing.
232 """
233
234 # Seeing what went wrong is trivial even without alignment if the expected
235 # string is very short. In this case do nothing to simplify output.
236 if len(s1) < 4:
237 return
238
239 maxw = 72 # Maximum number of characters shown
240
241 sys.stderr.write("Alignment of first line difference:\n")
242
243 trunc = False
244 while s1[:30] == s2[:30]:
245 s1 = s1[10:]
246 s2 = s2[10:]
247 trunc = True
248
249 if trunc:
250 s1 = "..." + s1
251 s2 = "..." + s2
252
253 max_len = max(len(s1), len(s2))
254 extra = ""
255 if max_len > maxw:
256 extra = "..."
257
258 # Write a chunk of both lines, aligned.
259 sys.stderr.write(f" E: {s1[:maxw]}{extra}\n")
260 sys.stderr.write(f" A: {s2[:maxw]}{extra}\n")
261 # Write an indicator character under the different columns.
262 sys.stderr.write(" ")
263 for j in range(min(maxw, max(len(s1), len(s2)))):
264 if s1[j : j + 1] != s2[j : j + 1]:
265 sys.stderr.write("^") # Difference
266 break
267 else:
268 sys.stderr.write(" ") # Equal
269 sys.stderr.write("\n")
270
271
272def clean_up(a: list[str]) -> list[str]:

Callers 1

Calls 5

lenFunction · 0.85
maxFunction · 0.85
rangeClass · 0.85
minFunction · 0.85
writeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…