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
111 stars 32 forks source link

Assignment to invalid type #44

Open balderman1 opened 4 years ago

balderman1 commented 4 years ago

I try to convert all values of a FlatterDict to strings. The email field caused an exception.

See below

data = {
    "x": "12",
    "other": "MLL",
    "channel": 33,
    "language": "EN",
    "email": [
        "jack@ecme.com"
    ]
}

flat = flatdict.FlatterDict(data)

for k in flat.keys():
    flat[k] = str(flat[k])

Error:

  File "C:\dev\tools\python371\lib\site-packages\flatdict.py", line 416, in __setitem__
    pk, self._delimiter, ck))
TypeError: Assignment to invalid type for key email:0
mileslucas commented 3 years ago

I'm also encountering this issue, it seems to happen only for list original types

Specifically this code: https://github.com/gmr/flatdict/blob/f86186671944e39ae194b49c5f444d69ffa37424/flatdict.py#L408-L418 L411 will fail because it is trying to split e.g.,

# key = "email:0" ->
a, b = "0".split(':', 1)

It seems like there either needs to be an extra check for a delimiter or not do the second split.