`Decimal128Field` raise `decimal.InvalidOperation` error occurs under certain circumstances
Opened this issue · 1 comments
When initializing a model with a Decimal128Field in the clickhouse_orm library, a decimal.InvalidOperation error occurs under certain circumstances.
Reproducible Example:
class TestDecimalModel(ClickHouseModel):
value = fields.Decimal128Field(scale=30, default=0)
a = TestDecimalModel(value=1)
Expected Behavior:
I expected the model TestDecimalModel to be initialized without any errors.
Environment Details:
Python version: 3.8.16
clickhouse_orm version: 2.1.0
Operating System: macOS 14.5
Additional Context:
The error seems to occur specifically when using a Decimal128Field with certain parameters. This issue might be related to how the to_python method handles the value.
from python docs
Unlike hardware based binary floating point, the decimal module has a user alterable precision (defaulting to 28 places) which can be as large as needed for a given problem:
Additional Context:
The following code can fix this.
I want to know if this is the best practice for fixing this issue.
class DecimalField(Field):
'''
Base class for all decimal fields. Can also be used directly.
'''
...
def _round(self, value):
with localcontext() as ctx:
ctx.prec = self.precision
return value.quantize(self.exp)
...