question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[bug] HOCONConverter relativedelta as value

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
ElkMonstercommented, 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.)

0reactions
dolfinuscommented, Dec 27, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · chimpler/pyhocon - GitHub
Overriding a value multiple times fails to resolve. #274 opened on Oct 25, ... [bug] HOCONConverter relativedelta as value. #246 opened on Oct...
Read more >
python dateutil relativedelta value out of range error
I am fond of dateutil module so i am just curious why it didn't work. Example date passed: 2014-02-26. Any ideas? python ·...
Read more >
relativedelta — dateutil 2.8.2 documentation - Read the Docs
The relativedelta type is designed to be applied to an existing datetime and ... value in the original datetime with the value(s) in...
Read more >
pyhocon Changelog - pyup.io
Support serializing timedelta and relativedelta to string (hocon, json etc.) ... fixed bug when we insert None values that shouldn't raise an exception...
Read more >
Python relativedelta to get duration of difference ... - Plus2net
day and days from dateutil.relativedelta import relativedelta from datetime import date dt=date.today() # today is 2019-09-23 # Adding one day to today ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found