Skip to content

Commit 866d86e

Browse files
committed
add tests
1 parent 629f1cf commit 866d86e

2 files changed

Lines changed: 38 additions & 6 deletions

File tree

mypyc/codegen/emit.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def object_annotation(self, obj: object, line: str) -> str:
210210
211211
If it contains illegal characters, an empty string is returned."""
212212
line_width = self._indent + len(line)
213-
formatted = pformat_deterministic(obj, line_width)
213+
formatted = pformat_deterministic(obj, max(90 - line_width, 20))
214214

215215
if any(x in formatted for x in ("/*", "*/", "\0")):
216216
return ""
@@ -1274,14 +1274,14 @@ def native_function_doc_initializer(func: FuncIR) -> str:
12741274
return c_string_initializer(docstring.encode("ascii", errors="backslashreplace"))
12751275

12761276

1277-
def pformat_deterministic(obj: object, line_width: int) -> str:
1277+
def pformat_deterministic(obj: object, width: int) -> str:
12781278
"""Pretty-print `obj` with deterministic sorting for mypyc literal types."""
1279-
# Temporarily override pprint._safe_key
1279+
# Temporarily override pprint._safe_key to get deterministic ordering of containers.
12801280
default_safe_key = pprint._safe_key # type: ignore [attr-defined]
12811281
pprint._safe_key = _mypyc_safe_key # type: ignore [attr-defined]
1282-
1282+
12831283
try:
1284-
return pprint.pformat(obj, compact=True, width=max(90 - line_width, 20))
1284+
return pprint.pformat(obj, compact=True, width=width)
12851285
finally:
12861286
# Always restore the original key to avoid affecting other pprint users.
12871287
pprint._safe_key = default_safe_key # type: ignore [attr-defined]

mypyc/test/test_emit.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from __future__ import annotations
22

3+
import pprint
34
import unittest
45

5-
from mypyc.codegen.emit import Emitter, EmitterContext
6+
from mypyc.codegen.emit import Emitter, EmitterContext, pformat_deterministic
67
from mypyc.common import HAVE_IMMORTAL
78
from mypyc.ir.class_ir import ClassIR
89
from mypyc.ir.ops import BasicBlock, Register, Value
@@ -21,6 +22,37 @@
2122
from mypyc.namegen import NameGenerator
2223

2324

25+
class TestPformatDeterministic(unittest.TestCase):
26+
def test_frozenset_elements_sorted(self) -> None:
27+
fs_small = frozenset({("a", 1)})
28+
fs_large = frozenset({("a", 1), ("b", 2)})
29+
literal_a = frozenset({fs_large, fs_small})
30+
literal_b = frozenset({fs_small, fs_large})
31+
expected = "frozenset({frozenset({('a', 1)}), frozenset({('a', 1), ('b', 2)})})"
32+
33+
assert pformat_deterministic(literal_a, 80) == expected
34+
assert pformat_deterministic(literal_b, 80) == expected
35+
36+
def test_nested_supported_literals(self) -> None:
37+
nested_frozen = frozenset({("m", 0), ("n", 1)})
38+
item_a = ("outer", 1, nested_frozen)
39+
item_b = ("outer", 2, frozenset({("x", 3)}))
40+
literal_a = frozenset({item_a, item_b})
41+
literal_b = frozenset({item_b, item_a})
42+
expected = (
43+
"frozenset({('outer', 1, frozenset({('m', 0), ('n', 1)})), "
44+
"('outer', 2, frozenset({('x', 3)}))})"
45+
)
46+
47+
assert pformat_deterministic(literal_a, 120) == expected
48+
assert pformat_deterministic(literal_b, 120) == expected
49+
50+
def test_restores_default_safe_key(self) -> None:
51+
original_safe_key = pprint._safe_key
52+
pformat_deterministic({"key": "value"}, 80)
53+
assert pprint._safe_key is original_safe_key
54+
55+
2456
class TestEmitter(unittest.TestCase):
2557
def setUp(self) -> None:
2658
self.n = Register(int_rprimitive, "n")

0 commit comments

Comments
 (0)