Skip to content

refactor: stop mutating self.variables() in the one-variable shortcut#22

Merged
hozblok merged 3 commits into
masterfrom
fix/08-solver-no-mutate-variables-set
May 19, 2026
Merged

refactor: stop mutating self.variables() in the one-variable shortcut#22
hozblok merged 3 commits into
masterfrom
fix/08-solver-no-mutate-variables-set

Conversation

@hozblok

@hozblok hozblok commented May 11, 2026

Copy link
Copy Markdown
Owner

Problem. Solver.__call__ extracted the sole variable name with set.pop() on the return value of self.variables(). Today that set is a fresh copy per call (so no harm is observable), but any future binding optimization that caches or returns a reference would have the wrapper silently corrupt internal state.

Fix. src/formula/formula.py:69-70 — use unpacking (only_var,) = variables so the set is never mutated.

Test. tests/test_solver_one_var_shortcut_invariants.py pins the invariant: solver.variables() returns the same set before and after the shortcut path, across repeated calls.

Full suite 319/319.

Solver.__call__ extracted the sole variable name with set.pop() on the
return value of self.variables(). Today that set is a fresh copy per
call, so no harm is observable — but any future binding optimization
that returns cached state would silently corrupt it. Use unpacking
((only_var,) = variables) so the set is never mutated.

Regression test in tests/test_solver_one_var_shortcut_invariants.py
asserts variables() is stable across the shortcut path.

See ai/improvements_2026-05-09.md item #8.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@hozblok hozblok marked this pull request as ready for review May 16, 2026 21:46
@hozblok hozblok merged commit 9783d22 into master May 19, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant