Why does beancount.core.data.Posting require an amount?

79 views
Skip to first unread message

Aaron Stacy

unread,
May 20, 2024, 9:24:45 AMMay 20
to Beancount
This ledger is valid even though the amount for the second posting of the transaction is omitted:

2024-01-01 open Assets:Checking
2024-01-01 open Expenses:Groceries

2024-01-01 * "Groceries"
  Expenses:Groceries 10 USD
  Assets:Checking

But Beancount's core data type for Posting requires a non-None amount for the units field, even though it's not a runtime error to pass None into it (example repro). I also notice, the beancount.loader module will insert an Amount when loading the Python objects. 

I can see why this might be desirable for code maintenance, but it seems to constrain the ledger unnecessarily. My question is should Posting#units have the type Optional[Amount]? I'd be happy to mail a pull request.

The background is that I upgraded to v3; I really love the nice static typing, but I've got a h̴e̴r̴d̴ ̴o̴f̴ ̴f̴e̴r̴a̴l̴ ̴c̴a̴t̴s̴ handful of scripts for importing transactions, and those create Posting objects to be output with beancount.parser.printer, and for that I prefer the cleaner ledger syntax of omitting the second posting's amount.

Daniele Nicolodi

unread,
May 20, 2024, 5:08:22 PMMay 20
to bean...@googlegroups.com
On 20/05/24 15:24, Aaron Stacy wrote:
> But Beancount's core data type for Posting requires a non-None amount
> for the units field

It is an error in the typing annotation. Feel free to send a PR.

> I also notice, the beancount.loader module will insert an Amount when loading the Python objects.

This is because beancount.loader also runs the plugins and does booking.
This requires the transactions to be completed, namely amounts for all
postings must be inferred. If you don't what that, you can load a ledger
into a Python data structure with beancount.parser.parse_file()

Cheers,
Dan

Aaron Stacy

unread,
May 21, 2024, 11:34:24 PMMay 21
to bean...@googlegroups.com
On Mon, May 20, 2024 at 4:08 PM Daniele Nicolodi <dan...@grinta.net> wrote:
On 20/05/24 15:24, Aaron Stacy wrote:
> But Beancount's core data type for Posting requires a non-None amount
> for the units field

It is an error in the typing annotation. Feel free to send a PR.




> I also notice, the beancount.loader module will insert an Amount when loading the Python objects.

This is because beancount.loader also runs the plugins and does booking.
This requires the transactions to be completed, namely amounts for all
postings must be inferred. If you don't what that, you can load a ledger
into a Python data structure with beancount.parser.parse_file()

Aaaah that makes sense. Thanks for the pointer!



Cheers,
Dan

--
You received this message because you are subscribed to a topic in the Google Groups "Beancount" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/beancount/PkeAxHwN-8U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to beancount+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beancount/0e0075fe-1fea-451d-b2bd-3629cf75f756%40grinta.net.
Reply all
Reply to author
Forward
0 new messages