Skip to content

Commit 346bcc7

Browse files
authored
Use frozendict with list-of-records (#4486)
It was like this before but somehow got removed. When there's a non-empty blob schedule, entries must be a frozendict so that it can be hashed.
1 parent 01454a2 commit 346bcc7

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

pysetup/md_to_spec.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,8 @@ def _process_list_of_records_table(self, table: Table, list_of_records_name: str
313313

314314
# Set the config variable
315315
self.spec["config_vars"][list_of_records_name] = VariableDefinition(
316-
"tuple[dict[str, Any], ...]",
317-
json.dumps(list_of_records_config_file, indent=4),
316+
"tuple[frozendict[str, Any], ...]",
317+
self._format_frozen_records(list_of_records_config_file),
318318
None,
319319
None,
320320
)
@@ -359,6 +359,17 @@ def _extract_list_of_records_spec(table: Table) -> list[dict[str, str]]:
359359

360360
return list_of_records_spec
361361

362+
@staticmethod
363+
def _format_frozen_records(records: list[dict[str, str]]) -> str:
364+
lines = ["("]
365+
for record in records:
366+
lines.append(" frozendict({")
367+
for key, value in record.items():
368+
lines.append(f' "{str(key)}": {str(value)},')
369+
lines.append(" }),")
370+
lines.append(")")
371+
return "\n".join(lines)
372+
362373
def _extract_typed_records_config(
363374
self, list_of_records_name: str, type_map: dict[str, str]
364375
) -> list[dict[str, str]]:

tests/infra/test_md_to_spec.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
from pathlib import Path
32

43
import pytest
@@ -110,12 +109,19 @@ def test_run_includes_list_of_records_table(tmp_path, dummy_preset, dummy_config
110109
# The result should have 'BLOB_SCHEDULE' in config_vars
111110
assert "BLOB_SCHEDULE" in spec_obj.config_vars
112111
# The value should be a list of dicts with type constructors applied
113-
var = json.loads(spec_obj.config_vars["BLOB_SCHEDULE"].value)
114-
assert isinstance(var, list)
115-
assert var[0]["EPOCH"] == "Epoch(269568)"
116-
assert var[0]["MAX_BLOBS_PER_BLOCK"] == "uint64(6)"
117-
assert var[1]["EPOCH"] == "Epoch(364032)"
118-
assert var[1]["MAX_BLOBS_PER_BLOCK"] == "uint64(9)"
112+
assert (
113+
spec_obj.config_vars["BLOB_SCHEDULE"].value
114+
== """(
115+
frozendict({
116+
"EPOCH": Epoch(269568),
117+
"MAX_BLOBS_PER_BLOCK": uint64(6),
118+
}),
119+
frozendict({
120+
"EPOCH": Epoch(364032),
121+
"MAX_BLOBS_PER_BLOCK": uint64(9),
122+
}),
123+
)"""
124+
)
119125

120126

121127
def test_run_includes_list_of_records_table_minimal(tmp_path, dummy_preset, dummy_config):
@@ -144,26 +150,33 @@ def test_run_includes_list_of_records_table_minimal(tmp_path, dummy_preset, dumm
144150
spec_obj = m2s.run()
145151
assert "BLOB_SCHEDULE" in spec_obj.config_vars
146152
# The result should follow the config, not the table
147-
var = json.loads(spec_obj.config_vars["BLOB_SCHEDULE"].value)
148-
assert isinstance(var, list)
149-
assert var[0]["EPOCH"] == "Epoch(2)"
150-
assert var[0]["MAX_BLOBS_PER_BLOCK"] == "uint64(3)"
151-
assert var[1]["EPOCH"] == "Epoch(4)"
152-
assert var[1]["MAX_BLOBS_PER_BLOCK"] == "uint64(5)"
153+
assert (
154+
spec_obj.config_vars["BLOB_SCHEDULE"].value
155+
== """(
156+
frozendict({
157+
"EPOCH": Epoch(2),
158+
"MAX_BLOBS_PER_BLOCK": uint64(3),
159+
}),
160+
frozendict({
161+
"EPOCH": Epoch(4),
162+
"MAX_BLOBS_PER_BLOCK": uint64(5),
163+
}),
164+
)"""
165+
)
153166

154167

155168
def test_run_includes_python_function(tmp_path, dummy_preset, dummy_config):
156-
md_content = """
169+
md_content = '''
157170
#### `compute_epoch_at_slot`
158171
159172
```python
160173
def compute_epoch_at_slot(slot: Slot) -> Epoch:
161-
\"\"\"
174+
"""
162175
Return the epoch number at slot.
163-
\"\"\"
176+
"""
164177
return Epoch(slot // SLOTS_PER_EPOCH)
165178
```
166-
"""
179+
'''
167180
file = tmp_path / "function.md"
168181
file.write_text(md_content)
169182
m2s = MarkdownToSpec(

0 commit comments

Comments
 (0)