Skip to content

Commit 27088bf

Browse files
committed
[mypyc] feat: improve LoadLiteral annotation determinism
1 parent 9dc611f commit 27088bf

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

mypyc/codegen/emit.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,15 @@ def object_annotation(self, obj: object, line: str) -> str:
208208
if any(x in formatted for x in ("/*", "*/", "\0")):
209209
return ""
210210

211+
# make frozenset annotations deterministic
212+
if formatted.startswith("frozenset({"):
213+
frozenset_items = formatted[11:-2]
214+
# if our frozenset contains another frozenset or a tuple, we will need better logic
215+
# here, but this redimentary logic will still vastly improve codegen determinism.
216+
if "(" not in frozenset_items:
217+
sorted_items = ", ".join(sorted(frozenset_items.split(", ")))
218+
formatted = "frozenset({" + sorted_items + "})"
219+
211220
if "\n" in formatted:
212221
first_line, rest = formatted.split("\n", maxsplit=1)
213222
comment_continued = textwrap.indent(rest, (line_width + 3) * " ")

0 commit comments

Comments
 (0)