jcrist / msgspec

A fast serialization and validation library, with builtin support for JSON, MessagePack, YAML, and TOML
https://jcristharif.com/msgspec/
BSD 3-Clause "New" or "Revised" License
2.01k stars 59 forks source link

Support coercing float to int if `strict=False` #619

Closed jcrist closed 6 months ago

jcrist commented 6 months ago

This adds a path for coercing floats to ints if strict=False, provided the float has an exact integral representation.

>>> import msgspec

>>> msgspec.convert(1.0, int, strict=False)
1

>>> msgspec.convert(1.5, int, strict=False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
msgspec.ValidationError: Expected `int`, got `float`

Fixes #613.