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

Key contains another key cause error #42

Open ghost opened 4 years ago

ghost commented 4 years ago

Problem

When a key contains the same string as previous key error below is thrown.

$ python test.py
Traceback (most recent call last):
  File "test.py", line 8, in <module>
    flatten = flatdict.FlatterDict(data, delimiter='_').items()
  File "/home/ctran/.venv/24x7/lib/python3.8/site-packages/flatdict.py", line 389, in __init__
    super(FlatterDict, self).__init__(value, delimiter, dict_class)
  File "/home/ctran/.venv/24x7/lib/python3.8/site-packages/flatdict.py", line 29, in __init__
    self.update(value)
  File "/home/ctran/.venv/24x7/lib/python3.8/site-packages/flatdict.py", line 356, in update
    [self.__setitem__(k, v) for k, v in dict(other or kwargs).items()]
  File "/home/ctran/.venv/24x7/lib/python3.8/site-packages/flatdict.py", line 356, in <listcomp>
    [self.__setitem__(k, v) for k, v in dict(other or kwargs).items()]
  File "/home/ctran/.venv/24x7/lib/python3.8/site-packages/flatdict.py", line 422, in __setitem__
    self._values[pk][ck] = value
  File "/home/ctran/.venv/24x7/lib/python3.8/site-packages/flatdict.py", line 420, in __setitem__
    raise TypeError(
TypeError: Assignment to invalid type for key key

Sample code

import flatdict

data = {
        'first_key': 'first_value',
        'first_key_again': 'first_value_again',
}

flatten = flatdict.FlatterDict(data, delimiter='_').items()
print(flatten)

Observed behaviors

When data is like below, error above is throw.

data = {
  'first_key': 'first_value',
  'first_key_again': 'first_value_again'
}

However, if data is below error isn't thrown but only first_key is returned. Key first_key_again is gone from the result.

data = {
  'first_key_again': 'first_value_again'
  'first_key': 'first_value',
}

$ python test.py
[('first_key', 'first_value')]
onc commented 1 year ago

This is still an issue :( It seems like this library is not actively maintained anymore.