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

List Flattening does not return list when an odd number of depth in the dictionary. #26

Closed mileslucas closed 5 years ago

mileslucas commented 5 years ago

I noticed a bug with FlatterDict today and am curious about how to try and tackle it. The problem is that if I have an iterable, if its depth is an odd number in the dictionary, the as_dict() method won't return it properly. For example:

values = {
    'foo': {
            'list': ['F', 'O', 'O'],
            'set': {10, 20, 30},
            'tuple': tuple(['F', 0, 0])
        }
}

works fine, but

values = {
    'foo': {
            'bar': {
                    'list': ['F', 'O', 'O'],
            },
            'set': {10, 20, 30},
            'tuple': tuple(['F', 0, 0])
        }
}

will not return list as a list. I've made a fork and added an instance like this to the tests. Looking through the code this seems to occur because in as_dict() the parent key and child key are always retrieved as a pair, even though in an odd-depth the parent key's original type might be an iterable.