beancount icon indicating copy to clipboard operation
beancount copied to clipboard

exception in any query using get_account_sort_key when localizing account names

Open azrdev opened this issue 3 years ago • 2 comments

I noticed the "holdings" tab in my fava only shows me the exception quoted below, clicking on "Query" and boiling down a minimal working example blames get_account_sort_key in conjunction with renamed (top-level) accounts.

exception:

$ bean-query mwe.beancount 'SELECT account, account_sortkey(account) '
Traceback (most recent call last):
  File "/usr/lib/python3.10/cmd.py", line 214, in onecmd
    func = getattr(self, 'do_' + cmd)
AttributeError: 'BQLShell' object has no attribute 'do_SELECT'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.10/site-packages/beancount/query/shell.py", line 271, in run_parser
    self.dispatch(statement)
  File "/usr/lib/python3.10/site-packages/beancount/query/shell.py", line 251, in dispatch
    return method(statement)
  File "/usr/lib/python3.10/site-packages/beancount/query/shell.py", line 416, in on_Select
    rtypes, rrows = query_execute.execute_query(c_query,
  File "/usr/lib/python3.10/site-packages/beancount/query/query_execute.py", line 282, in execute_query
    values = [c_expr(context) for c_expr in c_target_exprs]
  File "/usr/lib/python3.10/site-packages/beancount/query/query_execute.py", line 282, in <listcomp>
    values = [c_expr(context) for c_expr in c_target_exprs]
  File "/usr/lib/python3.10/site-packages/beancount/query/query_env.py", line 421, in __call__
    index, name = account_types.get_account_sort_key(context.account_types, args[0])
  File "/usr/lib/python3.10/site-packages/beancount/core/account_types.py", line 46, in get_account_sort_key
    return (account_types.index(get_account_type(account_name)), account_name)
ValueError: tuple.index(x): x not in tuple

MWE:

option "title" "MWE"
option "operating_currency" "EUR"
1992-01-01 custom "fava-option" "language" "de"

* Bilanzkonten
option "name_assets" "Aktiva"
option "name_liabilities" "Verbindlichkeiten"
option "name_equity" "Passiva:Eigenkapital"
option "account_previous_balances" "Passiva:Eigenkapital:Eröffnungssaldo"
option "account_current_earnings" "Gewinn"  ; net income
option "account_previous_earnings" "Rücklagen"  ; retained earnings
* Erfolgskonten
option "name_income" "EK:Einnahmen"
option "name_expenses" "EK:Ausgaben"

1992-01-01 open Passiva:Eigenkapital:Eröffnungssaldo
1992-01-01 open Aktiva:Giro

2000-01-01 * ""
  Aktiva:Giro  1000 EUR
  Passiva:Eigenkapital:Eröffnungssaldo

azrdev avatar Apr 08 '23 15:04 azrdev

The issue is not the renamed root accounts, but the fact that the account names assigned to the root accounts are leaf accounts. Namely, these options are invalid:

option "name_equity" "Passiva:Eigenkapital"
option "name_income" "EK:Einnahmen"
option "name_expenses" "EK:Ausgaben"

There cannot be an account name separator : in the names of these accounts. It seems that the validator for these options does not catch this error.

dnicolodi avatar Apr 08 '23 16:04 dnicolodi

+1 We should probably detect that and issue an appropriate error. (Keep this ticket open until we do)

blais avatar Apr 08 '23 16:04 blais