Skip to content

Generator refactor groundwork: golden tests + indentation context manager (PoC)#144

Merged
devdanzin merged 1 commit into
mainfrom
generator-golden-tests-and-indent-cm
Jun 29, 2026
Merged

Generator refactor groundwork: golden tests + indentation context manager (PoC)#144
devdanzin merged 1 commit into
mainfrom
generator-golden-tests-and-indent-cm

Conversation

@devdanzin

Copy link
Copy Markdown
Owner

Summary

Groundwork for migrating the code generators off the manual self.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 we pin all three and snapshot the generated source.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 (no MagicMock id leakage); a fake module with a public-only surface (test_private=False makes 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). The golden/ dir is ruff-excluded (generated fuzzer output, kept byte-exact).

Indentation context manager (fusil/write_code.py) — with self.indented(): replaces the saved = addLevel(1); …; restoreLevel(saved) (and error-prone restoreLevel(self.base_level - 1)) 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.

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.

Test plan

  • Full suite: 320 → 328 OK (+8: 5 write_code, 3 golden).
  • Golden snapshot byte-identical before and after the PoC conversion.
  • ruff check + ruff format --check clean (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

…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>
@devdanzin devdanzin merged commit 900e230 into main Jun 29, 2026
2 checks passed
@devdanzin devdanzin deleted the generator-golden-tests-and-indent-cm branch June 29, 2026 21:16
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.

Generator refactor groundwork: golden-output tests + indentation context manager (PoC)

1 participant