ssato / python-anyconfig

Python library provides common APIs to load and dump configuration files in various formats
MIT License
277 stars 31 forks source link

Too many values to unpack #112

Open xoxys opened 4 years ago

xoxys commented 4 years ago

Hi,

I try to merge some nested dicts like this:

... 'minio_prefix': {'value': {'minio_prefix': 'aaa'}} ... with ... ''minio_prefix': {'value': ['_unset_']} ... but get an exception:

Traceback (most recent call last):
  File "/home/org/user/Devel/private/python/ansible-doctor/env/bin/ansible-doctor", line 11, in <module>
    load_entry_point('ansible-doctor', 'console_scripts', 'ansible-doctor')()
  File "/home/org/user/Devel/private/python/ansible-doctor/ansibledoctor/__main__.py", line 8, in main
    AnsibleDoctor()
  File "/home/org/user/Devel/private/python/ansible-doctor/ansibledoctor/Cli.py", line 25, in __init__
    doc_parser = Parser()
  File "/home/org/user/Devel/private/python/ansible-doctor/ansibledoctor/DocumentationParser.py", line 30, in __init__
    self._populate_doc_data()
  File "/home/org/user/Devel/private/python/ansible-doctor/ansibledoctor/DocumentationParser.py", line 82, in _populate_doc_data
    anyconfig.merge(self._data, tags, ac_merge=anyconfig.MS_DICTS)
  File "/home/org/user/Devel/private/python/ansible-doctor/env/lib64/python3.7/site-packages/anyconfig/dicts.py", line 294, in merge
    _update_fn(self, other, key, **options)
  File "/home/org/user/Devel/private/python/ansible-doctor/env/lib64/python3.7/site-packages/anyconfig/dicts.py", line 234, in _update_with_merge
    merge(self[key], val, merge_lists=merge_lists, **options)
  File "/home/org/user/Devel/private/python/ansible-doctor/env/lib64/python3.7/site-packages/anyconfig/dicts.py", line 294, in merge
    _update_fn(self, other, key, **options)
  File "/home/org/user/Devel/private/python/ansible-doctor/env/lib64/python3.7/site-packages/anyconfig/dicts.py", line 234, in _update_with_merge
    merge(self[key], val, merge_lists=merge_lists, **options)
  File "/home/org/user/Devel/private/python/ansible-doctor/env/lib64/python3.7/site-packages/anyconfig/dicts.py", line 294, in merge
    _update_fn(self, other, key, **options)
  File "/home/org/user/Devel/private/python/ansible-doctor/env/lib64/python3.7/site-packages/anyconfig/dicts.py", line 234, in _update_with_merge
    merge(self[key], val, merge_lists=merge_lists, **options)
  File "/home/org/user/Devel/private/python/ansible-doctor/env/lib64/python3.7/site-packages/anyconfig/dicts.py", line 300, in merge
    raise type(exc)("%s other=%r" % (str(exc), other))
ValueError: too many values to unpack (expected 2) other=['_unset_']

Any suggestions how to work around it? If there is a type conflict like in this case (value: dict should be merged with value: list) I would expect a fallback to simply override it.

ssato commented 4 years ago

Thanks your report!

If there is a type conflict like in this case (value: dict should be merged with value: list) I would expect a fallback to simply override it.

As you know, there is no good default to merge a string with a list. Also type conflicts may indicate that given data are somehow invalid. So I don't want anyconfig behaving smart as you suggested and just let users decide what should be done for such cases.

In your case, I think it would be better if the later one is like ... ''minio_prefix': {'value': '']} ... or ... ''minio_prefix': {'value': 'unset'}.... (The identity element of monoid string is '' (empty string), I guess.)

xoxys commented 4 years ago

Thanks for your feedback. In this case it is not possible to refactor the later one because it is parsed from user input. But you are right, if the in put is malformed I should through an exception instead of some magic :)

ssato commented 4 years ago

Although I still believe anyconfig should not behave too 'smart' and hide any some unexpected cases it should not process originally, I changed my mind and think that it might be useful if the following merge strategies are available in anyconfig:

How do you think about this? If you like this idea and want, I'll try to implement those merge strategies.

xoxys commented 4 years ago

@ssato sorry for the late respons. I guess, having a merge strategy to handle such cases would be cool. How does these two described strategies handles current options for list append and dict merge?