Skip to content

Commit b286c11

Browse files
authored
Avoid repeated format of union members (#22297)
Codex claims this makes type checking pandas-stubs like 10% faster Co-authored-by Codex
1 parent 5b0b7f9 commit b286c11

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

‎mypy/messages.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,15 +2678,22 @@ def format_list(types: Sequence[Type]) -> str:
26782678
return ", ".join(format(typ) for typ in types)
26792679

26802680
def format_union_items(types: Sequence[Type]) -> list[str]:
2681-
formatted = [format(typ) for typ in types if format(typ) != "None"]
2681+
formatted = []
2682+
has_none = False
2683+
for typ in types:
2684+
item = format(typ)
2685+
if item == "None":
2686+
has_none = True
2687+
else:
2688+
formatted.append(item)
26822689
if len(formatted) > MAX_UNION_ITEMS and verbosity == 0:
26832690
more = len(formatted) - MAX_UNION_ITEMS // 2
26842691
formatted = formatted[: MAX_UNION_ITEMS // 2]
26852692
else:
26862693
more = 0
26872694
if more:
26882695
formatted.append(f"<{more} more items>")
2689-
if any(format(typ) == "None" for typ in types):
2696+
if has_none:
26902697
formatted.append("None")
26912698
return formatted
26922699

0 commit comments

Comments
 (0)