python-benedict: `from_ini()` returns a `str` instead of a `dict`

Python version Python 3.11.0

Package version python-benedict==0.30.0

Current behavior (bug description)

from benedict import benedict

original = benedict({
    'section1': {'key1': 'value1'},
    'sectionA': {'keyA': 'valueA', 'keyB': 'valueB', 'keyC': {'subkeyC': 'subvalueC'}},
})

readback = benedict.from_ini(original.to_ini())


print('original vs readback')

path = 'sectionA.keyC'

print(original.get(path), type(original.get(path)))
print('-- vs --')
print(readback.get(path), type(readback.get(path)))

output

original vs readback
{'subkeyC': 'subvalueC'} <class 'benedict.dicts.benedict'>
-- vs --
{'subkeyC': 'subvalueC'} <class 'str'>

Expected behavior value type of the keyC is a dict not a string

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 1
  • Comments: 21 (10 by maintainers)

Most upvoted comments

i guess the encoder doesn’t need any changes?

seems that encoder also must be changed

>>>  json.dumps({'key':True})
'{"key": true}'

in the ‘str’ key’s and values must be in double-quotes that json.loads works. and True must be lower-case in the ini-file

so the encoder must be also modified

as far as i understand ini files:

[section]
key=value

i guess the encoder doesn’t need any changes?

or what will happen when you always use in the INISerializer something like the following

pseudo-code

#encode() - write values into section of an ini
writeline(f"{key}={json.dumps(value)}"

#decode() - read lines/values from a section of an ini
section_value={}

for line in section:
  key, value = split(line) # e.g. use regex r"^(?P<key>\w+)\=(?P<value>.*?$"
  section_value[key] = json.loads(value)