Closed stevebyan closed 2 years ago
Hey there @stevebyan, thanks for posting!
I think in this case you'd want to overwrite/register a new custom decoding function for bool fields, and you'd probably want to use something like the str2bool
utility that is used with args from the command line.
Can you take a look at the simple_parsing.helpers.serialization.decoding
module? I think doing something like register_decoding_fn(bool, str2bool)
might just do it. If that works well, we could also use that as the default too.
Thanks again, and let me know if that helps!
Hey @stevebyan , sorry this took so long. This has been fixed now, it was a lot easier than I imagined in the end!
Describe the bug When parsing a json config file, SimpleParsing considers any string value for a boolean to be
True
. This is quite confusing to my users, who inadvertently write their json configs with quotes around the truth value, e.g."light_switch": "false"
. They are surprised when the dataclass fieldlight_switch
ends up containing the valueTrue
.To Reproduce
Expected behavior I expect either a liberal parsing of the json file:
or an error:
I expect an error when the json file contains some string value other than "true" or "false":
Actual behavior SimpleParsing treats any string value for the json field to be
True
.SimpleParsing could also treat non-string json values other than
true
orfalse
more gracefully:Desktop (please complete the following information):
Additional context Note that due to shell quoting,
--light_switch "false"
as a command line argument gives the correct result, and--light_switch '"false"'
gives the proper error, though the message would be more helpful if it indicated the value was a string rather than a boolean: