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

Function sort_messages_preserving_file_order

mypy/server/update.py:1322–1365  ·  view source on GitHub ↗

Sort messages so that the order of files is preserved. An update generates messages so that the files can be in a fairly arbitrary order. Preserve the order of files to avoid messages getting reshuffled continuously. If there are messages in additional files, sort them towards the

(
    messages: list[str], prev_messages: list[str]
)

Source from the content-addressed store, hash-verified

1320
1321
1322def sort_messages_preserving_file_order(
1323 messages: list[str], prev_messages: list[str]
1324) -> list[str]:
1325 """Sort messages so that the order of files is preserved.
1326
1327 An update generates messages so that the files can be in a fairly
1328 arbitrary order. Preserve the order of files to avoid messages
1329 getting reshuffled continuously. If there are messages in
1330 additional files, sort them towards the end.
1331 """
1332 # Calculate file order from the previous messages
1333 n = 0
1334 order = {}
1335 for msg in prev_messages:
1336 fnam = extract_fnam_from_message(msg)
1337 if fnam and fnam not in order:
1338 order[fnam] = n
1339 n += 1
1340
1341 # Related messages must be sorted as a group of successive lines
1342 groups = []
1343 i = 0
1344 while i < len(messages):
1345 msg = messages[i]
1346 maybe_fnam = extract_possible_fnam_from_message(msg)
1347 group = [msg]
1348 if maybe_fnam in order:
1349 # This looks like a file name. Collect all lines related to this message.
1350 while (
1351 i + 1 < len(messages)
1352 and extract_possible_fnam_from_message(messages[i + 1]) not in order
1353 and extract_fnam_from_message(messages[i + 1]) is None
1354 and not messages[i + 1].startswith("mypy: ")
1355 ):
1356 i += 1
1357 group.append(messages[i])
1358 groups.append((order.get(maybe_fnam, n), group))
1359 i += 1
1360
1361 groups = sorted(groups, key=lambda g: g[0])
1362 result = []
1363 for key, group in groups:
1364 result.extend(group)
1365 return result

Callers 5

test_simple_sortingMethod · 0.90
updateMethod · 0.85

Calls 8

lenFunction · 0.85
sortedFunction · 0.85
appendMethod · 0.80
extendMethod · 0.80
startswithMethod · 0.45
getMethod · 0.45

Tested by 4

test_simple_sortingMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…