filters: remove_opening_balance_txn does replacement instead of del.
This commit is contained in:
parent
5601ece2ac
commit
95ba1638d2
2 changed files with 13 additions and 2 deletions
|
@ -87,5 +87,12 @@ def remove_opening_balance_txn(entries: Entries) -> Optional[Transaction]:
|
|||
break
|
||||
else:
|
||||
return None
|
||||
del entries[index]
|
||||
# Deleting from the beginning of a list is O(n) slow in Python:
|
||||
# <https://wiki.python.org/moin/TimeComplexity>
|
||||
# So don't do that, and instead replace the transaction with a placeholder
|
||||
# directive.
|
||||
# The type:ignore is because of the funny way Beancount builds directives.
|
||||
entries[index] = bc_data.Custom( # type:ignore[operator]
|
||||
entry.meta, entry.date, "Removed opening balances", [],
|
||||
)
|
||||
return entry
|
||||
|
|
|
@ -153,5 +153,9 @@ def test_remove_opening_balance_txn(opening_txn):
|
|||
entries.insert(1, opening_txn)
|
||||
actual = filters.remove_opening_balance_txn(entries)
|
||||
assert actual is opening_txn
|
||||
assert len(entries) == 2
|
||||
assert opening_txn not in entries
|
||||
assert not any(
|
||||
post.account.startswith('Equity:')
|
||||
for entry in entries
|
||||
for post in getattr(entry, 'postings', ())
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue