Skip to content

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

Merged
jcrist merged 1 commit into
mainfrom
strict-false-float-as-int
Dec 20, 2023
Merged

jcrist merged 1 commit into
mainfrom
strict-false-float-as-int

Conversation

@jcrist

@jcrist jcrist commented Dec 20, 2023

Copy link
Copy Markdown
Member

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.

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`
```
@jcrist
jcrist merged commit 9fb503a into main Dec 20, 2023
@jcrist
jcrist deleted the strict-false-float-as-int branch December 20, 2023 03:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

decode's strict=False does not cast floats to ints

1 participant