aleo.codegen: build-time ABI→Python emitter - #47
Merged
Conversation
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…e test Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
- validate ABI identifiers before interpolating into emitted source (keywords, non-identifiers, reserved _nonce -> generation-time ValueError) - reject cross-program/undefined struct references and duplicate struct names at emit time instead of emitting NameError-at-import modules - strict plaintext parsing: missing separators and malformed member names now raise instead of silently gluing tokens into strings - clear TypeError/ValueError for None/'null'/empty mapping values - accept signed field/group/scalar literals in fmt_fieldlike (-1field) - emit the full ABI dict constant (mapping key types stay recoverable) - fixture provenance (_source) per AGENTS.md; drop git-ignored spec path from the package docstring Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
kpandl
approved these changes
Jul 13, 2026
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.
What
A contract-agnostic, build-time code generator inside the main SDK:
aleo.codegenturns anyaleo-abiJSON description of a program into a Python module of frozen dataclasses withto_plaintext()encoders andfrom_plaintext()decoders — the Python analog of aleo-viem'sveil-codegen, minus the typed contract factory (the facade'sPreparedCall+ hand-written verbs fill that role in Python).python -m aleo.codegen --abi shield_swap.abi.json --out _generated.py python -m aleo.codegen --config codegen.config.json # multi-program modeEmitted per program: struct dataclasses (encode + decode), record dataclasses (decode-only — scanners produce them, callers never construct them;
_noncerides along), aMAPPING_VALUE_DECODERStable,PROGRAM_ID, and the fullABIdict (so mapping key types stay recoverable at runtime, like the TS bindings'PROGRAM_ABI).Why
Two things an ABI can't provide at runtime: static types (
slot.sqrt_pricecompleting in an IDE, pyright catching typos) and struct input encoding — the facade's_coerce_onedeliberately rejects dicts, so struct-typed function arguments must arrive as preformatted plaintext strings. Generatedto_plaintext()is how a typed client builds them. First consumer: the upcomingaleo-shield-swappackage (follow-up PR).Layout
aleo/codegen/runtime.py— stdlib-only plaintext parser + literal formatters that generated modules import (strict grammar: malformed plaintext raises instead of silently gluing tokens;None/nullmapping values get clear errors)aleo/codegen/_emit.py— ABI ty-tree → source emitter; validates every ABI identifier before interpolation (keywords / injection / reserved_nonce→ generation-timeValueError), rejects cross-program and duplicate struct references that would emitNameError-at-import modules, topo-sorts nested structsaleo/codegen/__main__.py— CLIshield_swap_v3.aleoABI (12 structs / 4 records / 19 mappings, vendored with_sourceprovenance) and round-trips live-shaped plaintextsVerification
python -m pytest python/tests -m "not slow"— 839 passed (19 new)npx pyrightonpython/aleo/codegen— 0 errorsBuild-time only: nothing in the
aleoruntime imports this package, and no new dependencies.🤖 Generated with Claude Code