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

[bug] HOCONConverter relativedelta as value #246

Open
dolfinus opened this issue Oct 12, 2020 · 6 comments
Open

[bug] HOCONConverter relativedelta as value #246

dolfinus opened this issue Oct 12, 2020 · 6 comments

Comments

@dolfinus
Copy link

dolfinus commented Oct 12, 2020

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.

@dolfinus
Copy link
Author

P.S. Also I don't see any tag corresponding to versions above 0.3.54 in this repo. Can you add them, please?

@dolfinus
Copy link
Author

@darthbear Could you check it please?

@dolfinus
Copy link
Author

dolfinus commented Jul 13, 2021

#263 is a step towards fixing this issue, but it does not fully solve it:
https://github.com/chimpler/pyhocon/pull/263/files#r668600206

@ElkMonster
Copy link

ElkMonster commented Aug 2, 2021

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.)

@dolfinus
Copy link
Author

dolfinus commented Aug 3, 2021

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.

@dolfinus
Copy link
Author

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

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

No branches or pull requests

2 participants