Changeset - 01c3b975d811
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-04-12 02:26:01
brettcsmith@brettcsmith.org
data: Fix Amount.__new__.

See the comments for background and rationale.
2 files changed with 16 insertions and 6 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/data.py
Show inline comments
...
 
@@ -134,2 +134,11 @@ class Amount(bc_amount.Amount):
 

	
 
    # 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 <https://github.com/python/mypy/issues/1279>.
 
    # 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)
 

	
 

	
...
 
@@ -311,3 +320,3 @@ def balance_of(txn: Transaction,
 
        currency = weights[0].currency
 
    return Amount._make((number, currency))
 
    return Amount(number, currency)
 

	
tests/testutil.py
Show inline comments
...
 
@@ -76,8 +76,8 @@ 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,
...
 
@@ -134,4 +134,5 @@ class Transaction:
 
        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)
 

	
0 comments (0 inline, 0 general)