Skip to content

feat(plugin): add realization and compare to the Python compat layer - #2592

Merged
robcohen merged 2 commits into
mainfrom
feat/python-shim-realization-compare
Oct 11, 2026
Merged

robcohen merged 2 commits into
mainfrom
feat/python-shim-realization-compare

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

The Python compat layer now provides two more beancount modules, ported from beancount 3:

  • beancount.core.realization: RealAccount (a dict of child components with account, txn_postings, balance), realize(entries, min_accounts=None, compute_balance=...), iter_children(real, leaf_only=False), get, get_or_create, contains and postings_by_account. postings_by_account follows beancount: postings as TxnPosting, and open, close, balance, note, document, pad (both accounts) and custom account values as entries.
  • beancount.core.compare: CompareError, stable_hash_namedtuple, hash_entry, hash_entries (duplicate prices allowed, as in beancount), compare_entries, includes_entries, excludes_entries.

realize with balances (compute_balance=True, its default) needs beancount.core.inventory, which the layer does not have. It raises NotImplementedError saying to pass compute_balance=False (as leafonly does), rather than returning a tree without balances.

Why

Beancount's own leafonly and noduplicates import these modules and stopped with cannot import name 'realization' from 'beancount.core'.

How tested

  • New conformance case python_compat_realization_and_compare:

    • a plugin written the way beancount's leafonly is (realize + iter_children + getters.get_account_open_close) reports ledger.beancount:3:1: ... Non-leaf account 'Assets:Bank' has postings on it and exercises get, contains and iter_children(leaf_only=True);
    • a noduplicates-style plugin flags a duplicated transaction once, at the copy's line, and checks includes_entries both ways;
    • realize(entries) gives the NotImplementedError.

    It fails without the change (the import fails).

  • Beancount 3's actual leafonly.py and noduplicates.py, copied from the installed package and run under this build as plugin "leafonly.py" / "noduplicates.py", report what bean-check reports. That includes beancount's leafonly reporting a parent named only by a note or custom. These files are not vendored: beancount is GPLv2, this repo is GPL-3.0-only.

  • cargo test -p rustledger-plugin --all-features and -p rustledger --all-features (with RLEDGER_REQUIRE_*) pass. clippy (workspace, all features) is clean, and so are fmt and cargo doc -D warnings. Release-config clippy has only the wasm_plugins_dir dead-code error, which main already has; the docs: release binaries do not include plugin runtimes #2541 PR fixes it.

Closes #2540

🤖 Generated with Claude Code

https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr

Beancount's own leafonly and noduplicates import
beancount.core.realization and beancount.core.compare, which the compat
layer lacked, so both stopped with an ImportError. Both modules are now
ported from beancount 3: realization's account tree (realize with
compute_balance=False, iter_children, get, get_or_create, contains,
postings_by_account, RealAccount) and compare's stable entry hashes
(hash_entry, hash_entries, compare_entries, includes_entries,
excludes_entries). realize with balances needs beancount.core.inventory
and raises NotImplementedError saying so.

Closes #2540

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@robcohen
robcohen requested a review from a team as a code owner October 10, 2026 22:15
@github-actions

github-actions Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 51.4ms ± 1.4ms 739.5ms ± 10.5ms 14.4x 51.3 MB / 49.9 MB
investment 76.8ms ± 1.2ms 2766.2ms ± 43.7ms 36.0x 57.4 MB / 58.1 MB
fifo 75.3ms ± 1.0ms 16078.6ms ± 429.0ms 213.7x 57.4 MB / 57.8 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 50.3ms ± 0.7ms 51.4ms ± 1.4ms +2.2% ✅ within noise 51.3 MB → 51.3 MB (+0.0%)
investment 77.0ms ± 0.9ms 76.8ms ± 1.2ms -0.3% ✅ within noise 57.4 MB → 57.4 MB (+0.0%)
fifo 74.9ms ± 1.0ms 75.3ms ± 1.0ms +0.4% ✅ within noise 59.4 MB → 57.4 MB (-3.2%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 0.7ms, investment 0.7ms, fifo 0.6ms), and is at least 5%.
10,000 transactions per shape · 20 runs, 3 warmup · hyperfine

What do these numbers mean?
  • simple — Two-posting transfers, one currency. The historical workload — keep as the floor, not as the benchmark.
  • investment — Cost specs, per-unit prices and lot reductions. Exercises booking and inventory, which simple leaves at 0.3%.
  • fifo — Bare {} reductions against a FIFO account — how FIFO sells are normally written. The other four shapes reach the ordered-selection path twice in 2k transactions between them, so it was effectively unprofiled until it turned out to be quadratic (plan_ordered scans every lot per reduction: FIFO ledgers are quadratic (8.8s at 20k transactions) #2083).
  • vs base: the PR and the base-branch commit it is built on, built and measured in the same job
  • RSS: peak resident set size, a single sample — reported without a verdict because there is no spread to test it against
  • A shape with no beancount column is one bean-check reports errors on; it is timed for rledger only

@codecov

codecov Bot commented Oct 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 179 masked)
Full AST Match 100%
Total Files 819
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
    The headline is the EFFECTIVE rate, after pairs where beanquery
    itself is wrong are masked. raw is the same run with nothing
    masked. A widening gap means the registry grew, not that rledger
    changed.
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

@robcohen

Copy link
Copy Markdown
Member Author

Review loop

Round Scope Finding Fix
1 The ported modules checked line by line against beancount 3's realization.py and compare.py: postings_by_account (pads on both accounts, custom account values in both beancount's form and this layer's), iter_children order, the price exemption in hash_entries, the sort used for missing lists. Oracle: beancount's own leafonly.py and noduplicates.py, unchanged under this build, against bean-check on the conformance ledgers and on a ledger with a note, a custom and a pad on parent accounts none; identical diagnostics and locations n/a

The loop converged at round 1. The required checks are green on 72f5323.

@robcohen
robcohen merged commit ce70dce into main Oct 11, 2026
53 of 55 checks passed
@robcohen
robcohen deleted the feat/python-shim-realization-compare branch October 11, 2026 09:05
robcohen added a commit that referenced this pull request Oct 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.

plugin: add realization and compare to the Python compat shim

1 participant