Skip to content

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

Closed

Description

@thomasjpfan

Description

On msgspec==0.18.4, strict=False does not cast floats to ints:

import msgspec

msgspec.json.decode(b'[1, 2, 3.0]', type=list[int], strict=False)
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
Cell In[38], line 3
      1 import msgspec
----> 3 msgspec.json.decode(b'[1, 2, 3.0]', type=list[int], strict=False)

ValidationError: Expected `int`, got `float` - at `$[2]`

Is this expected behavior with strict=False?

Activity

  1. jcrist commented on Dec 13, 2023

    @jcrist
    Member

    Currently, yeah. I could see adding support for coercing floats without a decimal component to integers though if needed (so 3.0 would be valid, but 3.5 would error). Can you say more about your use case here?

  2. thomasjpfan commented on Dec 13, 2023

    @thomasjpfan
    Author

    I'm ingesting in a format that deserializes all numerical values to floats, while the original type was an int. Since the original data were ints the floats end up being: 1.0, 6.0, etc.

    At a high level, the data serialization is:

    1. Original data in Python that contain ints
    2. Serialize Python data into protobuf (the ints are preserved in this format)
    3. Deserialize custom format into a json (where the ints gets converted into floats)
    4. msgspec to convert json back into a Python type

    I sense there is a bug in 3, so I likely do not require msgspec casting floats into ints.

  3. jcrist commented on Dec 20, 2023

    @jcrist
    Member

    I've added support for this in #619. Even though you said this likely wasn't necessary, it felt like something we could/should support (and supporting it was easy to do). Thanks for raising the issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions