Generator refactor groundwork: golden tests + indentation context manager (PoC)#144
Merged
Merged
Conversation
…ager Foundation for migrating the code generators off the manual write()/addLevel()/ restoreLevel() indentation idiom (the systemic complexity driver in doc/code-review-findings.md). Golden-output harness (tests/python/test_golden_output.py): WritePythonCode is a pure function of (RNG seed, options, target module), so pin all three and snapshot the generated source.py. Determinism is engineered -- fixed seed, fully-pinned options (concrete values, no MagicMock id leakage), a fake module with a public-only surface (test_private=False makes the generator skip dunders, so introspection is version-stable), and no_numpy/no_tstrings so output is independent of installed deps. Three checks: determinism+parse (all versions), the converted method's block (version-independent), and a full-script snapshot (pinned to the canonical >=3.14). The golden/ dir is ruff-excluded (it's generated fuzzer output, kept byte-exact). Indentation context manager (fusil/write_code.py): `with self.indented():` replaces the addLevel(1)/restoreLevel(saved) bookkeeping with a scoped block whose nesting mirrors the generated code and can't leak a level. Named `indented` (not `indent`) because `self.indent` is the indent string. Covered by tests/test_write_code.py (restore-on-exit, restore-on-exception, nesting, equivalence with the manual dance). Proof of concept: the class-instantiation try/except/if triple-dance in write_python_code.py now uses `with self.indented():`; the golden snapshot proves the generated output is byte-identical. Suite 320 -> 328 OK; ruff check + format clean. Closes #143 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Groundwork for migrating the code generators off the manual
self.write()/addLevel()/restoreLevel()indentation idiom (the systemic complexity driver indoc/code-review-findings.md).Golden-output harness (
tests/python/test_golden_output.py) —WritePythonCodeis a pure function of (RNG seed, options, target module), so we pin all three and snapshot the generatedsource.py. This is the regression net that lets generator refactors be proven byte-for-byte behaviour-preserving. Determinism is engineered: fixed seed; fully-pinned options (noMagicMockid leakage); a fake module with a public-only surface (test_private=Falsemakes the generator skip dunders → version-stable introspection);no_numpy/no_tstrings→ output independent of installed deps. Three checks: determinism+parse (all versions), the converted method's block (version-independent), and a full-script snapshot (pinned to the canonical>=3.14). Thegolden/dir is ruff-excluded (generated fuzzer output, kept byte-exact).Indentation context manager (
fusil/write_code.py) —with self.indented():replaces thesaved = addLevel(1); …; restoreLevel(saved)(and error-pronerestoreLevel(self.base_level - 1)) bookkeeping with a scoped block whose nesting mirrors the generated code and can't leak a level. Namedindented(notindent) becauseself.indentis the indent string. Covered bytests/test_write_code.py.Proof of concept — the class-instantiation
try/except/iftriple-dance inwrite_python_code.pynow useswith self.indented():; the golden snapshot proves the generated output is byte-identical.Test plan
write_code, 3 golden).ruff check+ruff format --checkclean (golden fixture excluded).This unblocks the incremental generator cleanup: convert methods to
with self.indented():(and templates for fixed-shape blocks) one at a time, with the golden net catching any drift.Closes #143