gmr / flatdict

Python module for interacting with nested dicts as a single level dict with delimited keys.
https://flatdict.readthedocs.io
BSD 3-Clause "New" or "Revised" License
112 stars 32 forks source link

__setitem__ does not work on a dictionary of `lists` for `FlatterDict` #51

Open darioisola opened 2 years ago

darioisola commented 2 years ago

The following

import flatdict

fdoc = flatdict.FlatterDict({'a': [3, 4]})
fdoc['a:0'] = 1

should set the first element of the list a but it raises the an error

Traceback (most recent call last):
  File "/home/dario/miniconda3/envs/rotore_for_commit_devel/lib/python3.8/site-packages/flatdict.py", line 411, in __setitem__
    k, cck = ck.split(self._delimiter, 1)
ValueError: not enough values to unpack (expected 2, got 1)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/home/dario/miniconda3/envs/rotore_for_commit_devel/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3397, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-cf8840d6b1c9>", line 1, in <cell line: 1>
    runfile('/home/dario/Codes/rotor_design_for_commit/activities/tiltrotor_tip/submit.py', wdir='/home/dario/Codes/rotor_design_for_commit/activities/tiltrotor_tip')
  File "/home/dario/Software/pycharm-2021.2.3/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 198, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/home/dario/Software/pycharm-2021.2.3/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/dario/Codes/rotor_design_for_commit/activities/tiltrotor_tip/submit.py", line 23, in <module>
    fdoc['a:0'] = 1
  File "/home/dario/miniconda3/envs/rotore_for_commit_devel/lib/python3.8/site-packages/flatdict.py", line 414, in __setitem__
    raise TypeError(
TypeError: Assignment to invalid type for key a:0

which I can be fixed in various ways. I assume this is not the intended behavior.

abibas commented 2 years ago

According to my undertanding - this is not expected behavior. It's clearly stated in the examples from the main page, that LISTs are supported:

value = flatdict.FlatterDict({'list': ['a', 'b', 'c']})

In any case we needed to fix it for our need, but I have a strange feeling that PR won't be accepted. I can see PRs are not processed since 9 Feb 2021. Example: https://github.com/gmr/flatdict/pull/45

Here is my PR https://github.com/gmr/flatdict/pull/52