Review fix: freeze the engine's overrides snapshot
Review round 1 (Codex gpt-5.6-terra, MEDIUM): the overrides mapping was shallow-copied, leaving caller-owned lists mutable through _overrides — a later append would change is_present results. Each validated list is copied into an immutable tuple before wrapping; tests prove post-construction appends change nothing and the snapshot refuses mutation. Suite: 996 passed.
This commit is contained in:
@@ -574,3 +574,19 @@ def test_engine_overlapping_snapshot_uses_latest_start() -> None:
|
||||
assert engine.is_present(1, datetime.date(2026, 1, 9)) is True
|
||||
# Jan 11-12: only the later override covers -> present.
|
||||
assert engine.is_present(1, datetime.date(2026, 1, 11)) is True
|
||||
|
||||
|
||||
def test_engine_override_lists_are_frozen() -> None:
|
||||
"""The overrides snapshot must be truly read-only: mutating the
|
||||
caller's list after construction must not change answers."""
|
||||
entry = PresenceOverride(1, "2026-01-06", "2026-01-06", False)
|
||||
entries = [entry]
|
||||
engine = _engine_with_overrides(*entries)
|
||||
assert engine.is_present(1, datetime.date(2026, 1, 6)) is False
|
||||
entries.append(PresenceOverride(1, "2026-01-07", "2026-01-07", False))
|
||||
# The late append must NOT change the engine's answers...
|
||||
assert engine.is_present(1, datetime.date(2026, 1, 7)) is True
|
||||
with pytest.raises(TypeError):
|
||||
engine._overrides[1].append(entry)
|
||||
with pytest.raises(AttributeError, match="immutable snapshot"):
|
||||
engine._overrides = {}
|
||||
|
||||
Reference in New Issue
Block a user