Skip to content

Add proper tests for astcompile - #651

Merged
antocuni merged 7 commits into
mainfrom
astcompile-test
Aug 11, 2026
Merged

Add proper tests for astcompile#651
antocuni merged 7 commits into
mainfrom
astcompile-test

Conversation

@antocuni

@antocuni antocuni commented Aug 11, 2026

Copy link
Copy Markdown
Member

This should have belonged to #649 , but too late and too bad.

The hard part is how to write readable tests, because comparing the generated AST is a pain. We ideally want to use the SPy backend to generated "astcompiled source code", but then all the nuances of NameLocalDirect, AssignLocal, etc, disappears.

The solution was to introduce a new option for the SPy backend ast_format="full", which annotates some selected nodes with extra info needed for tests.
This is now exposed also from the spy cli.
E.g.:

❯ cat /tmp/x.spy
def main() -> None:
    x = 1
    print(x)

with the default "human-readable" format, we don't see any difference:

❯ spy astcompile -f spy /tmp/x.spy
def main() -> None:
    x = 1
    print(x)

But with --full-ast, we do!

❯ spy astcompile -f spy /tmp/x.spy --full-ast
def main() -> None:
    AssignLocal(x := 1)
    ImportRef(print)(LocalDirect(x))

antocuni and others added 3 commits August 11, 2026 12:55
- Add AST_FORMAT option to SPyBackend with "short" (default) and "full" modes
- In "full" mode, show lowered node types: LocalDirect(x), OuterCell(x),
  AssignLocal(x := val), etc.
- Change NameError formatting to show "raise NameError('name')"
- Add test_astcompile.py with tests for:
  - NameError lowering
  - for loop desugaring to while
  - Name resolution (LocalDirect, ImportRef, OuterCell, OuterDirect)
  - Assignment lowering (AssignLocal, AssignCell)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
When enabled, shows full AST node types in output (e.g., LocalDirect(x),
OuterCell(x), AssignLocal(x := val)).

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Aug 11, 2026

Copy link
Copy Markdown
Documentation Preview
Preview documentation for PR #651.
Last Updated: 2026-08-11 11:26 UTC

@antocuni
antocuni merged commit a9b4aa1 into main Aug 11, 2026
8 checks passed
@antocuni
antocuni deleted the astcompile-test branch August 11, 2026 11:32
@antocuni antocuni mentioned this pull request Aug 11, 2026
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