Open tbm opened 9 years ago
Original comment by Justus Pendleton (Bitbucket: hoostus, GitHub: hoostus).
I've spent a while going through the code in query_render.py gradually (mostly, sorta, kinda) figuring out how the PositionRenderer -> AmountRenderer -> DecimalRenderer stuff all hangs together. To be honest, I don't (yet, at least) totally understand what problem there is with ignoring the key in DecimalRenderer.format(). But during the course of looking over the code I think I've come across a bigger issue, DecimalRender is mixing up global scope (i.e. all transactions in the bean file) and local scope (i.e. just the transactions in the query result set).
As I began to understand the rendering code I began to wonder...why is DecimalRenderer doing all this work anyway? It seems like the whole thing can be ripped out and replaced with a DisplayFormatter using Align.DOT (and turning off commas).
#!python
def format(self, number, key=None):
display_formatter = self.dcontext.build(alignment=display_context.Align.DOT, commas=False)
return display_formatter.format(number)
Doing this doesn't result in exactly the same output, however. And investigating why that is led me to realise: in DecimalRenderer.update() there's a call to the global display context
#!python
def update(self, number, key=None):
if number is None:
return
# Quantize the number based on the display context.
qnumber = self.dcontext.quantize(number, key)
However, the rest of DecimalRenderer is building up what is effectively a DisplayFormatter but with a scope of just the query result set. What exactly does that mean and what are the consequences?
Imagine:
The results of Precision.MOST_COMMON or Precision.MAXIMUM will now vary.
#!python
self.dcontext.quantize(number, key) == 4
self.dists[key].mode() == 2
So it seems clear (to me at least!) that using the global DisplayContext but constructing a local ad-hoc formatter in DecimalFormatter is wrong. The question is which of the two needs to be fixed:
I can start working on a patch once there's some agreement on which direction is best.
Original report by Martin Blais (Bitbucket: blais, GitHub: blais).
See beancount.query.query_render.DecimalRenderer.format(). It ignores key. This could explain a lot of the inconsistencies I've been witnessing.