pyhocon
pyhocon copied to clipboard
[bug] HOCONConverter relativedelta as value
Before 0.3.51:
from pyhocon import ConfigFactory, HOCONConverter
conf = ConfigFactory.parse_string('key: 10s')
# ConfigTree([('key', '10s')])
HOCONConverter.to_json(conf)
# '{\n "key": "10s"\n}'
HOCONConverter.to_yaml(conf)
# 'key: 10s'
HOCONConverter.to_hocon(conf)
# 'key = "10s"'
HOCONConverter.to_properties(conf)
# 'key = 10s'
After:
from pyhocon import ConfigFactory, HOCONConverter
conf = ConfigFactory.parse_string('key: 10s')
# ConfigTree([('key', relativedelta(seconds=+10))])
HOCONConverter.to_json(conf)
# '{\n "key": relativedelta(seconds=+10)\n}'
HOCONConverter.to_yaml(conf)
# 'key: relativedelta(seconds=+10)'
HOCONConverter.to_hocon(conf)
# 'key = relativedelta(seconds=+10)'
HOCONConverter.to_properties(conf)
# 'key = relativedelta(seconds=+10)'
It's not possible to use result of such export in other languages/applications without custom config parsers. IMHO the best option here is to add some option to parser to enable/disable such cast of config values.
P.S. Also I don't see any tag corresponding to versions above 0.3.54 in this repo. Can you add them, please?
@darthbear Could you check it please?
#263 is a step towards fixing this issue, but it does not fully solve it: https://github.com/chimpler/pyhocon/pull/263/files#r668600206
I think the main problem here is that, if I'm not mistaken, pyhocon does not keep the original document, but converts everything to what it believes to be the native data type, right when parsing. If conversion between data types is lossless, that's no problem (e.g. '1' -> 1 -> '1'), but in case of periods of time (or what looks like it), converting back does not work anymore.
I think get_string() would need to operate on a separate data structure that provides the original value strings. With the current implementation, not only does one not have access to the original string value, but also there's no way to have values that look like periods of time.
E.g. I might want to store a hash in a config that happens to contain only numbers and a letter at the end:
hash = 123456789h
get_string('hash') returns "relativedelta(days=+5.14403e+06, hours=+21)". I don't see how one could deal with that… so sadly I think pyhocon must be considered broken in that respect.
(The only option is to explicitly declare the value to be a string by enclosing it in "", but that may not be a valid thing to do in some use cases.)
Thank you @ElkMonster, that's a precise issue description.
Unfortunately, it looks like pyhocon's magic is not compatible with such an approach, so it will never be able to save files in a format some applications need.
Moved to https://github.com/omry/omegaconf, it does support many features present in HOCON but it does not mess up with parsing date types