def _apply(self, value):
allowed_types = (text_type, int, float, DecimalType,)
if self.allow_tuples:
# Python's Decimal type supports both tuples and lists.
# :py:meth:`decimal.Decimal.__init__`
allowed_types += (list, tuple,)
value = self._filter(value, Type(allowed_types))
if self._has_errors:
return value
try:
d = DecimalType(value)
except (InvalidOperation, TypeError, ValueError):
return self._invalid_value(value, self.CODE_INVALID, exc_info=True)
# Decimal's constructor also accepts values such as 'NaN' or
# '+Inf', which aren't valid in this context.
# :see: decimal.Decimal._parser
if not d.is_finite():
return self._invalid_value(
value = value,
reason = self.CODE_NON_FINITE,
exc_info = True,
)
if self.max_precision is not None:
d = d.quantize(self.max_precision)
return d
评论列表
文章目录