Changeset - c6dc2d83aca7
[Not reviewed]
1 7 0
Brett Smith - 4 years ago 2020-04-09 16:00:38
brettcsmith@brettcsmith.org
data.Amount: Introduce class and simplify code to use it.

See docstring for full rationale. This greatly reduces the need for other
plugin code to handle the case of `post.units.number is None`, eliminating
the need for entire methods and letting it do plain numeric comparisons.
8 files changed with 46 insertions and 231 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/data.py
Show inline comments
...
 
@@ -26,2 +26,3 @@ import operator
 
from beancount.core import account as bc_account
 
from beancount.core import amount as bc_amount
 

	
...
 
@@ -116,2 +117,18 @@ class Account(str):
 

	
 
class Amount(bc_amount.Amount):
 
    """Beancount amount after processing
 

	
 
    Beancount's native Amount class declares number to be Optional[Decimal],
 
    because the number is None when Beancount first parses a posting that does
 
    not have an amount, because the user wants it to be automatically balanced.
 

	
 
    As part of the loading process, Beancount replaces those None numbers
 
    with the calculated amount, so it will always be a Decimal. This class
 
    overrides the type declaration accordingly, so the type checker knows
 
    that our code doesn't have to consider the possibility that number is
 
    None.
 
    """
 
    number: decimal.Decimal
 

	
 

	
 
class Metadata(MutableMapping[MetaKey, MetaValue]):
...
 
@@ -223,2 +240,4 @@ class Posting(BasePosting):
 
    * The `account` field is an Account object
 
    * The `units` field is our Amount object (which simply declares that the
 
      number is always a Decimal—see that docstring for details)
 
    * The `meta` field is a PostingMeta object
...
 
@@ -228,2 +247,3 @@ class Posting(BasePosting):
 
    account: Account
 
    units: Amount
 
    # mypy correctly complains that our MutableMapping is not compatible
...
 
@@ -236,30 +256,2 @@ class Posting(BasePosting):
 

	
 
    def _compare_amount(self,
 
                        op: Callable[[decimal.Decimal], decimal.Decimal],
 
                        threshold: DecimalCompat,
 
                        default: Optional[bool],
 
    ) -> Optional[bool]:
 
        if self.units.number is None:
 
            return default
 
        else:
 
            return op(self.units.number) > threshold
 

	
 
    def is_credit(self,
 
                  threshold: DecimalCompat=0,
 
                  default: Optional[bool]=None,
 
    ) -> Optional[bool]:
 
        return self._compare_amount(operator.pos, threshold, default)
 

	
 
    def is_debit(self,
 
                  threshold: DecimalCompat=0,
 
                  default: Optional[bool]=None,
 
    ) -> Optional[bool]:
 
        return self._compare_amount(operator.neg, threshold, default)
 

	
 
    def is_payment(self,
 
                  threshold: DecimalCompat=0,
 
                  default: Optional[bool]=None,
 
    ) -> Optional[bool]:
 
        return self.account.is_cash_equivalent() and self.is_debit(threshold, default)
 

	
 

	
...
 
@@ -267,4 +259,3 @@ def balance_of(txn: Transaction,
 
               *preds: Callable[[Account], Optional[bool]],
 
               default: Optional[DecimalCompat]=None,
 
) -> Optional[decimal.Decimal]:
 
) -> decimal.Decimal:
 
    """Return the balance of specified postings in a transaction.
...
 
@@ -274,14 +265,8 @@ def balance_of(txn: Transaction,
 
    match any of the predicates.
 

	
 
    If any of the postings have no amount, returns default.
 
    """
 
    retval = decimal.Decimal(0)
 
    for post in txn.postings:
 
        acct = Account(post.account)
 
        if any(p(acct) for p in preds):
 
            if post.units.number is None:
 
                return None if default is None else decimal.Decimal(default)
 
            else:
 
                retval += post.units.number
 
    return retval
 
    return sum(
 
        (post.units.number for post in iter_postings(txn)
 
         if any(pred(post.account) for pred in preds)),
 
        decimal.Decimal(0),
 
    )
 

	
conservancy_beancount/plugin/meta_approval.py
Show inline comments
...
 
@@ -33,15 +33,15 @@ class MetaApproval(core._RequireLinksPostingMetadataHook):
 
    def _run_on_txn(self, txn: Transaction) -> bool:
 
        if not super()._run_on_txn(txn):
 
            return False
 
        # approval is required when funds leave a cash equivalent asset,
 
        # UNLESS that transaction is a transfer to another asset,
 
        # or paying off a credit card.
 
        balance = data.balance_of(
 
            txn,
 
            data.Account.is_cash_equivalent,
 
            data.Account.is_credit_card,
 
        return (
 
            super()._run_on_txn(txn)
 
            # approval is required when funds leave a cash equivalent asset,
 
            # UNLESS that transaction is a transfer to another asset,
 
            # or paying off a credit card.
 
            and self.payment_threshold > data.balance_of(
 
                txn,
 
                data.Account.is_cash_equivalent,
 
                data.Account.is_credit_card,
 
            )
 
        )
 
        return balance is None or balance < self.payment_threshold
 

	
 
    def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool:
 
        return post.account.is_cash_equivalent() and not post.is_credit(0)
 
        return post.account.is_cash_equivalent() and post.units.number < 0
conservancy_beancount/plugin/meta_payable_documentation.py
Show inline comments
...
 
@@ -29,3 +29,3 @@ class MetaPayableDocumentation(core._RequireLinksPostingMetadataHook):
 
        if post.account.is_under('Liabilities:Payable:Accounts'):
 
            return not post.is_credit()
 
            return post.units.number < 0
 
        else:
conservancy_beancount/plugin/meta_receipt.py
Show inline comments
...
 
@@ -41,3 +41,2 @@ class MetaReceipt(core._RequireLinksPostingMetadataHook):
 
            and not post.account.is_under('Assets:PayPal')
 
            and post.units.number is not None
 
            and abs(post.units.number) >= self.payment_threshold
...
 
@@ -68,3 +67,3 @@ class MetaReceipt(core._RequireLinksPostingMetadataHook):
 
        is_checking = post.account.is_checking()
 
        if is_checking and post.is_debit():
 
        if is_checking and post.units.number < 0:
 
            return self._run_checking_debit(txn, post)
...
 
@@ -72,3 +71,3 @@ class MetaReceipt(core._RequireLinksPostingMetadataHook):
 
            keys.append('check')
 
        elif post.account.is_credit_card() and not post.is_credit():
 
        elif post.account.is_credit_card() and post.units.number <= 0:
 
            keys.append('invoice')
conservancy_beancount/plugin/meta_receivable_documentation.py
Show inline comments
...
 
@@ -56,3 +56,3 @@ class MetaReceivableDocumentation(core._RequireLinksPostingMetadataHook):
 
            return False
 
        elif post.is_debit():
 
        elif post.units.number < 0:
 
            return False
conservancy_beancount/plugin/meta_tax_implication.py
Show inline comments
...
 
@@ -47,5 +47,8 @@ class MetaTaxImplication(core._NormalizePostingMetadataHook):
 
    def __init__(self, config: configmod.Config) -> None:
 
        self.payment_threshold = config.payment_threshold()
 
        self.payment_threshold = -config.payment_threshold()
 

	
 
    def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool:
 
        return post.is_payment(self.payment_threshold) is not False
 
        return (
 
            post.account.is_cash_equivalent()
 
            and post.units.number < self.payment_threshold
 
        )
tests/test_data_balance_of.py
Show inline comments
...
 
@@ -36,19 +36,2 @@ def payable_payment_txn():
 

	
 
@pytest.fixture
 
def none_posting_txn():
 
    return testutil.Transaction(postings=[
 
        ('Income:Donations', -30),
 
        ('Expenses:BankingFees', 3),
 
        ('Assets:Checking', None),
 
    ])
 

	
 
@pytest.fixture
 
def multipost_one_none_txn():
 
    return testutil.Transaction(postings=[
 
        ('Liabilities:Payable:Accounts', 50),
 
        ('Assets:Checking', -50),
 
        ('Expenses:BankingFees', 5),
 
        ('Assets:Checking', None),
 
    ])
 

	
 
def balance_under(txn, *accts):
...
 
@@ -84,23 +67 @@ def test_balance_of_multipost_txn(payable_payment_txn):
 
    assert data.balance_of(payable_payment_txn, is_cash_eq) == -55
 

	
 
def test_balance_of_none_posting(none_posting_txn):
 
    assert data.balance_of(none_posting_txn, is_cash_eq) is None
 

	
 
def test_balance_of_none_posting_with_default(none_posting_txn):
 
    expected = Decimal('Infinity')
 
    assert expected == data.balance_of(
 
        none_posting_txn, is_cash_eq, default=expected,
 
    )
 

	
 
def test_balance_of_other_side_of_none_posting(none_posting_txn):
 
    assert balance_under(none_posting_txn, 'Income') == -30
 
    assert balance_under(none_posting_txn, 'Expenses') == 3
 

	
 
def test_balance_of_multi_postings_one_none(multipost_one_none_txn):
 
    assert data.balance_of(multipost_one_none_txn, is_cash_eq) is None
 

	
 
def test_balance_of_multi_postings_one_none(multipost_one_none_txn):
 
    expected = Decimal('Infinity')
 
    assert expected == data.balance_of(
 
        multipost_one_none_txn, is_cash_eq, default=expected,
 
    )
tests/test_data_posting.py
Show inline comments
 
deleted file
0 comments (0 inline, 0 general)