Implement quantization for Decimal type when encode#978
Implement quantization for Decimal type when encode#978NyanFisher wants to merge 1 commit intojcrist:mainfrom
Conversation
2357ed5 to
076b58c
Compare
2ee9741 to
31effee
Compare
117001b to
b5ff6b7
Compare
|
CI failures are unrelated to this change:
All build, test, and wheel jobs pass across all platforms. |
b5ff6b7 to
09b8d01
Compare
|
Code looks solid and CI is green across the matrix - nice work, especially the test coverage in One API design question I'd like to raise before this moves forward: the current shape places An alternative would be to attach quantization to the type via Price = Annotated[Decimal, Meta(decimal_quantize="0.0001", decimal_rounding="ROUND_HALF_EVEN")]That composes naturally with per-field configuration, lives next to the type where the constraint is logically defined, and matches how Did you consider the cc @jcrist @ofek — this expands the encoder API surface, so I'd like your read on whether the encoder-kwarg shape is the one we want, or whether |
|
Instead of two new options for quantization, how about adding a single # Uses default rounding
enc = Encoder(decimal_format="0.0001")
# Custom rounding
enc = Encoder(decimal_format=lambda d: d.quanitize(decimal.Decimal("0.001"), "ROUND_DOWN"))I like this since it's more flexible, and also only adds a single new option. Otherwise I'd worry about other users needing further customization, resulting in a number of I wouldn't expect a callable here to have a perf cost - calling into python here is negligible, most of the time will be in the
In For now a single setting on an |
|
@Siyet @jcrist Hello! Thanks for the review!
I hadn't considered using
I like this idea, but the |
|
After thinking it through I'm coming around to @jcrist's single-kwarg shape. One slot for everything is, in my view, the right call here. Encoder(decimal_format=lambda d: d.quantize(Decimal("0.001"), ROUND_DOWN)) # custom
Encoder(decimal_format="string") # existing
Encoder(decimal_format="number") # existingWe could split it along There is also the naming angle: @NyanFisher regarding your concern about overloading an existing kwarg: the three shapes ( |
Hello!
Description of the problem solved by this PR
The msgspec library has many useful features, but the current version lacks the ability to correctly quantize Decimal values during encoding. In applications related to finance and precise calculations, it is critical to take into account maximum accuracy and return values rounded to a specified precision. Without implementing this functionality, a complete transition from pydantic to msgspec is not possible.
Changes implemented in this PR
This improvement enhances the functionality of the library and ensures full compatibility when transitioning to msgspec in financial and other applications that require precise control over the representation of decimal numbers.
Examples
decimal_quantize
decimal_rounding
I would appreciate any comments on improving or restructuring the code, as I don't often write in C.
Fix my issue - Closes #848