-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-5508 - Add built-in DecimalEncoder and DecimalDecoder #2499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
bson/decimal128.py
Outdated
"""Converts Python :class:`decimal.Decimal` to BSON :class:`Decimal128`. | ||
|
||
.. warning:: When converting BSON data types to and from built-in data types, | ||
the possibility of data loss is always present due to mismatches in underlying implementations. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"possibility of data loss is always present" is extremely alarming. We have tests that do auto-convert and they don't have data loss issues so I think the language is too strong. Can we identify and explain exactly where the risk is and when it would occur?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Decimal128 supports up to 34 decimal digits of precision, decimal.Decimal by default supports up to 28 digit during arithmetic operations. When creating a new decimal.Decimal value, that 28 digit limit does not exist, so I don't think we need any warning here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it seems like the truncation issue will be caught on the encoding side:
>>> Decimal128(decimal.Decimal('1'*34))
Decimal128('1111111111111111111111111111111111')
>>> Decimal128(decimal.Decimal('1'*35))
Traceback (most recent call last):
File "<python-input-6>", line 1, in <module>
Decimal128(decimal.Decimal('1'*35))
~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/shane/git/mongo-python-driver/bson/decimal128.py", line 218, in __init__
self.__high, self.__low = _decimal_to_128(value)
~~~~~~~~~~~~~~~^^^^^^^
File "/Users/shane/git/mongo-python-driver/bson/decimal128.py", line 76, in _decimal_to_128
value = ctx.create_decimal(value)
decimal.Inexact: [<class 'decimal.Inexact'>]
doc/changelog.rst
Outdated
Changes in Version 4.15.0 (XXXX/XX/XX) | ||
-------------------------------------- | ||
.. warning:: When converting BSON data types to and from built-in data types, the possibility of data loss is always present | ||
due to mismatches in underlying implementations. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure we need this warning here too.
return Decimal128(value) | ||
|
||
|
||
class DecimalDecoder(TypeDecoder): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we add a docstring example showing out to use these classes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
No description provided.