Skip to content

Commit 6a29c59

Browse files
authored
feat(pyamber): reject non-string state keys (#8959)
### What changes were proposed in this PR? Reject non-string state dictionary keys before serialization. Converting both 1 and "1" to the same JSON key silently loses a value. This introduces an explicit TypeError for non-string keys, including nested mappings. ### Any related issues, documentation, discussions? Closes #8215 Replaces #8216, which the stale bot closed. The original PR cannot be reopened by this account. ### How was this PR tested? Tested on freshly fetched main at d27b7b4 with Python 3.12 on Windows. The regression failed before the source change and passed after it. From the amber directory, with generated protobuf bindings available on PYTHONPATH: ```text python -m pytest src/test/python/core/models/test_state.py::TestState::test_to_json_rejects_non_string_keys -q --tb=line -p no:cacheprovider python -m pytest src/test/python/core/models src/test/python/core/architecture/sendsemantics/test_partitioners.py --deselect src/test/python/core/models/test_tuple.py::TestTuple::test_hash -q --tb=line -p no:cacheprovider ruff check src/main/python src/test/python ruff format --check src/main/python src/test/python ``` The broader suites passed 351 tests with one expected failure. Ruff lint and formatting passed for all 216 Python files. The existing Windows pre-epoch hash fixture was deselected; the new hash regressions were run separately. ### Was this PR authored or co-authored using generative AI tooling? Generated-by: Codex (GPT-6)
1 parent 9cf43f2 commit 6a29c59

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

‎amber/src/main/python/core/models/state.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ def _to_json_value(value: Any) -> Any:
9393
_PAYLOAD_MARKER: base64.b64encode(value).decode("ascii"),
9494
}
9595
if isinstance(value, dict):
96-
return {str(key): _to_json_value(inner) for key, inner in value.items()}
96+
if any(not isinstance(key, str) for key in value):
97+
raise TypeError("State dictionary keys must be strings")
98+
return {key: _to_json_value(inner) for key, inner in value.items()}
9799
if isinstance(value, (list, tuple)):
98100
return [_to_json_value(inner) for inner in value]
99101
raise TypeError(

‎amber/src/test/python/core/models/test_state.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ class Custom:
9090
with pytest.raises(TypeError):
9191
State({"bad": Custom()}).to_json()
9292

93+
@pytest.mark.parametrize(
94+
"state", [State({1: "value"}), State({"nested": {1: "value"}})]
95+
)
96+
def test_to_json_rejects_non_string_keys(self, state):
97+
with pytest.raises(TypeError, match="keys must be strings"):
98+
state.to_json()
99+
93100
def test_tuple_round_trip(self):
94101
original = State({"i": 3, "label": "outer", "blob": b"\x01\x02"})
95102
decoded = State.from_tuple(original.to_tuple())

0 commit comments

Comments
 (0)