-
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,8 +20,11 @@ | |
|
||
import decimal | ||
import struct | ||
from decimal import Decimal | ||
from typing import Any, Sequence, Tuple, Type, Union | ||
|
||
from bson.codec_options import TypeDecoder, TypeEncoder | ||
|
||
_PACK_64 = struct.Struct("<Q").pack | ||
_UNPACK_64 = struct.Struct("<Q").unpack | ||
|
||
|
@@ -58,6 +61,38 @@ | |
_VALUE_OPTIONS = Union[decimal.Decimal, float, str, Tuple[int, Sequence[int], int]] | ||
|
||
|
||
class DecimalEncoder(TypeEncoder): | ||
"""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. | ||
|
||
.. versionadded:: 4.15""" | ||
|
||
@property | ||
def python_type(self): | ||
return Decimal | ||
|
||
def transform_python(self, value): | ||
return Decimal128(value) | ||
|
||
|
||
class DecimalDecoder(TypeDecoder): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add a docstring example showing out to use these classes? |
||
"""Converts BSON :class:`Decimal128` to Python :class:`decimal.Decimal`. | ||
|
||
.. 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. | ||
|
||
.. versionadded:: 4.15""" | ||
|
||
@property | ||
def bson_type(self): | ||
return Decimal128 | ||
|
||
def transform_bson(self, value): | ||
return value.to_decimal() | ||
|
||
|
||
def create_decimal128_context() -> decimal.Context: | ||
"""Returns an instance of :class:`decimal.Context` appropriate | ||
for working with IEEE-754 128-bit decimal floating point values. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
Changelog | ||
========= | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure we need this warning here too. |
||
|
||
PyMongo 4.15 brings a number of changes including: | ||
|
||
- Added :class:`bson.decimal128.DecimalEncoder` and :class:`bson.decimal128.DecimalDecoder` | ||
to support encoding and decoding of BSON Decimal128 values to decimal.Decimal values using the TypeRegistry API. | ||
|
||
Changes in Version 4.14.1 (2025/08/19) | ||
-------------------------------------- | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Uh oh!
There was an error while loading. Please reload this page.
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: