PYTHON-5395 - Add convert_decimal to CodecOptions#2491
PYTHON-5395 - Add convert_decimal to CodecOptions#2491NoahStapp wants to merge 8 commits intomongodb:masterfrom
Conversation
|
Will update the |
sleepyStick
left a comment
There was a problem hiding this comment.
Just a couple of minor comments
| options->unicode_decode_error_handler = NULL; | ||
|
|
||
| if (!PyArg_ParseTuple(options_obj, "ObbzOOb", | ||
| if (!PyArg_ParseTuple(options_obj, "ObbzOObb", |
There was a problem hiding this comment.
I stared at this for a while but then found: https://docs.python.org/3/c-api/arg.html. So, just confirming that's a format string for the args below.
There was a problem hiding this comment.
lol i was sitting on this one too, googled it, found that same site, was still confused / overwhelemed by words so then i asked mongo-gpt HAHA
There was a problem hiding this comment.
Yup this is a format string.
| in_fallback_call); | ||
| Py_DECREF(binary_value); | ||
| return result; | ||
| } else if (options->convert_decimal && _check_decimal(value)) { |
There was a problem hiding this comment.
So after case 100 or so and if case 19 wasn't the case, auto-convert ?
There was a problem hiding this comment.
For consistency with how the rest of _write_element_to_buffer works, we check the converison edge cases such as this after the native BSON case statement.
ShaneHarvey
left a comment
There was a problem hiding this comment.
This PR doesn't add new functionality since users can already configure a CodecOptions that automatically encodes Decimal, for example:
class DecimalEncoder(TypeEncoder):
@property
def python_type(self):
return Decimal
def transform_python(self, value):
return Decimal128(value)
DECIMAL_CODECOPTS = CodecOptions(type_registry=TypeRegistry([DecimalEncoder()]))TypeRegistry can also be configured to auto-decode Decimal128 to decimal by implementing TypeDecoder.
Given this, is it necessary to add this convert_decimal flag?
| converted = Decimal128(value) | ||
| return b"\x13" + name + converted.bid | ||
| else: | ||
| raise InvalidDocument("decimal.Decimal must be converted to bson.decimal128.Decimal128.") |
There was a problem hiding this comment.
Suggest mentioning the convert_decimal option in this error message.
The I'm fine to make this auto-conversion the default instead of adding a flag, but then we would explicitly disallow users from making their own custom Encoders for |
|
The Decimal128 specification explicitly does not allow this kind of auto-conversion. Closed. |
Context
We currently reject Python
decimal.Decimalinstances and require the user to manually convert to Decimal128.This PR adds a new
convert_decimalkwarg to CodecOptions that enables automatic conversion ofdecimal.DecimaltoDecimal128during BSON encoding.Summary and example of changes
Current behavior and new behavior when
convert_decimal=False(default)New behavior when
convert_decimal=TrueTesting
test/test_bson.pyhas a new testTestCodecOptions.test_convert_decimalto verify correctness.A changelog entry will be added in this PR if we determine that this is the correct approach to move ahead with.