You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 68f53a7
Browse filesBrowse the repository at this point in the historyBrowse files
Add callable support for decimal_format option (msgspec#978)
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
### Core Functionality
- Added DECIMAL_FORMAT_CALLABLE enum value to support callable
decimal_format
- Added decimal_callable field to EncoderState and Encoder structs
- Added `in_decimal_callable` field to `EncoderState` struct
- Added recursion protection in `mpack_encode_decimal()` and
`json_encode_decimal()`
- Implemented callable invocation for both JSON and MessagePack encoders
### Validation & Safety
- Added runtime check preventing callable from returning Decimal (avoids
infinite recursion)
- Direct `Decimal` returns: `lambda d: d`
- Nested structures: `lambda d: [d]`, `lambda d: {"key": d}`
### Type Hints
- Updated json.pyi and msgpack.pyi stubs to include callable type hints:
```python
decimal_format: Union[
Literal["string", "number"],
Callable[[decimal.Decimal], Any],
]
```
## Examples
#### Rounding to 2 Decimal Places
```python
import msgspec
import decimal
enc = msgspec.json.Encoder(
decimal_format=lambda d: str(d.quantize(decimal.Decimal("0.01")))
)
value = decimal.Decimal("123.456789")
print(enc.encode(value)) # b'"123.46"'
```
#### MessagePack with Rounding
```python
import msgspec
import decimal
# MessagePack with custom rounding
enc = msgspec.msgpack.Encoder(
decimal_format=lambda d: float(d.quantize(decimal.Decimal("0.001")))
)
value = decimal.Decimal("3.14159265")
msg = enc.encode(value)
print(msgspec.msgpack.decode(msg)) # 3.142
```
#### Error: Returning Decimal from Callable
```python
import msgspec
import decimal
# INVALID: callable must not return Decimal
enc = msgspec.json.Encoder(
decimal_format=lambda d: d.quantize(decimal.Decimal("0.01")) # Error!
)
try:
enc.encode(decimal.Decimal("1.234"))
except TypeError as e:
print(e) # callable returned a value containing a Decimal
```
I would appreciate any comments on improving or restructuring the code,
as I don't often write in C.
Fix my issue - Closesmsgspec#848
---------
Co-authored-by: Siyet <[email protected]>
0 commit comments