diff --git a/conservancy_beancount/data.py b/conservancy_beancount/data.py index 86a06c7f57c808309019c954603f8997481d7124..b6903d30ec98aae486514d2e41efd997baa5eab9 100644 --- a/conservancy_beancount/data.py +++ b/conservancy_beancount/data.py @@ -132,6 +132,15 @@ class Amount(bc_amount.Amount): """ number: decimal.Decimal + # beancount.core._Amount is the plain namedtuple. + # beancore.core.Amount adds instance methods to it. + # b.c.Amount.__New__ calls `b.c._Amount.__new__`, which confuses type + # checking. See . + # It works fine if you use super(), which is better practice anyway. + # So we override __new__ just to call _Amount.__new__ this way. + def __new__(cls, number: decimal.Decimal, currency: str) -> 'Amount': + return super(bc_amount._Amount, Amount).__new__(cls, number, currency) + class Metadata(MutableMapping[MetaKey, MetaValue]): """Transaction or posting metadata @@ -309,7 +318,7 @@ def balance_of(txn: Transaction, ] number = sum((wt.number for wt in weights), number) currency = weights[0].currency - return Amount._make((number, currency)) + return Amount(number, currency) @functools.lru_cache() def is_opening_balance_txn(txn: Transaction) -> bool: diff --git a/tests/testutil.py b/tests/testutil.py index 87e68fc16c582379b63f37e74165e810e8add7b3..c33519af142b547be42f06332485ae584adc99c1 100644 --- a/tests/testutil.py +++ b/tests/testutil.py @@ -74,12 +74,12 @@ def Cost(number, currency='USD', date=FY_MID_DATE, label=None): def Posting(account, number, currency='USD', cost=None, price=None, flag=None, - **meta): + type_=bc_data.Posting, **meta): if cost is not None: cost = Cost(*cost) - if meta is None: + if not meta: meta = None - return bc_data.Posting( + return type_( account, Amount(number, currency), cost, @@ -132,8 +132,9 @@ class Transaction: 'lineno': 0, } self.meta.update(meta) - for posting in postings: - self.add_posting(*posting) + if postings is not None: + for posting in postings: + self.add_posting(*posting) def add_posting(self, arg, *args, **kwargs): """Add a posting to this transaction. Use any of these forms: