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

dict(FlatDict) fails when it contains complex unicode keys #19

Closed iliapolo closed 6 years ago

iliapolo commented 6 years ago

There seem to be various issues when I put a complex unicode key inside the flatdict.

For example:

import flatcdict
d = flatdict.Flatdict()
d[u'key1:key2'] = 'value1'
dict(d)

KeyError: u'key1'

The same thing works if the key is a string object

import flatcdict
d = flatdict.Flatdict()
d['key1:key2'] = 'value1'
dict(d)

{'key1:key2': 'value1'}

I tracked the issue to this line where you force the key to be an 'str' object for it to have a delimiter.

Any special reason for that?

def _has_delimiter(self, key):
    """Checks to see if the key contains the delimiter.

    :rtype: bool

    """
    return isinstance(key, str) and self._delimiter in key

Its a problem for me because I populate the flatdict with objects returned by the json.loads function, which uses unicode for keys.

Environment

Python 2.7.14 on macOS HighSierra 10.13.3

I'll be happy to contribute and fix this issue once we discuss it.

Thanks 👍

iliapolo commented 6 years ago

Well, I just realised that in Python 3, unicode was renamed to str. So is it just a compatibility issue with Python 2.7?

gmr commented 6 years ago

@iliapolo probably, I'll take a look. Given the looming EOL for Python 2.7 (<2 years), I've been coding Python 3 first, and probably just missed this.

gmr commented 6 years ago

This should be resolved in v3.0.1