redstreet/beancount_reds_importers

bug: ot.unit_price AttributeError when unit_price is not set

kmac opened this issue · 2 comments

kmac commented

This code results in an AttributeError, because ot.unit_price is immutable.

ot.unit_price = round(abs(ot.total) / ot.units, 4)

            # annoyingly, vanguard reinvests have unit_price set to zero. so manually compute it
            if (hasattr(ot, 'security') and ot.security) and ot.units and not ot.unit_price:
                ot.unit_price = round(abs(ot.total) / ot.units, 4)
            common.create_simple_posting_with_cost(entry, main_acct, units, ticker, ot.unit_price,
                                                   self.currency, self.price_cost_both_zero_handler)

It's a simple fix, just set a local variable unit_price and pass that into the create_simple_posting_with_cost directly. I don't think ot.unit_price is used beyond that:

            unit_price = ot.unit_price
            # annoyingly, vanguard reinvests have unit_price set to zero. so manually compute it
            if (hasattr(ot, 'security') and ot.security) and ot.units and not ot.unit_price:
                unit_price = round(abs(ot.total) / ot.units, 4)
            common.create_simple_posting_with_cost(entry, main_acct, units, ticker, unit_price,
                                                   self.currency, self.price_cost_both_zero_handler)

Thanks for finding this and filing! Would you be able to submit a pull request?

Also, if you don't mind pasting the entire stack trace? Thanks!

Done, thanks for filing!