Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialization of Union[float, bool] fields #264

Open
halirutan opened this issue Jun 13, 2023 · 1 comment
Open

Serialization of Union[float, bool] fields #264

halirutan opened this issue Jun 13, 2023 · 1 comment
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@halirutan
Copy link

I'm "enhancing" existing code and have to work with some of the more questionable choices when it comes to types.
In this particular case, a field can be either bool or float. When using serialization, it revealed a deficiency that I try to understand.

To Reproduce

from dataclasses import dataclass
from typing import Union
from simple_parsing import Serializable


@dataclass
class MyOptions(Serializable):
    value: Union[float, bool] = False


opt1 = MyOptions()
opt1.save("/tmp/opt1.json")
opt1_loaded = MyOptions.load("/tmp/opt1.json")
print(opt1_loaded)

When executing this, the value is deserialized as 0.0 and I also get a warning that this might happen.
What I don't understand is that a custom decoding function is absolutely straightforward and does the trick. Why is the default behavior not able to handle such cases?

Here is what I used as decoding function

from simple_parsing.helpers.serialization import register_decoding_fn

def decode_boolean_float(raw_value):
    if isinstance(raw_value, float):
        return raw_value
    elif isinstance(raw_value, bool):
        return raw_value
    else:
        raise RuntimeError(f"Could not decode JSON value {raw_value}")

register_decoding_fn(Union[float, bool], decode_boolean_float)

Desktop macOS:

  • Version 0.1.3
  • Python 3.9.16
@lebrice
Copy link
Owner

lebrice commented Jun 19, 2023

Hi @halirutan , thanks for posting!

This is a nice bug. Indeed, I'd expect this to work as expected out-of-the-box. Good catch!

@lebrice lebrice added bug Something isn't working good first issue Good for newcomers labels Jun 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants