makinacorpus / easydict

Access dict values as attributes (works recursively)
GNU Lesser General Public License v3.0
293 stars 48 forks source link

Working with non-stringable keys — lets allow it, but just dont set attributes. #40

Open belonesox opened 10 months ago

belonesox commented 10 months ago

Lets consider an example:


from easydict import EasyDict as edict

adict_widh_non_str_keys_and_dicts_inside = {
    ('Lon', 'Arn'): {'a':1, 'b':2},
    ('Lon', 'Gou'): {'a':3, 'b':4},
}

aedict = edict(adict_widh_non_str_keys_and_dicts_inside)

Now we get exception:

… /site-packages/easydict/__init__.py", line 122, in __init__
    setattr(self, k, v)
TypeError: attribute name must be string, not 'tuple'

Obviously there is not mapping/processing for «non-stringable» keys, and it is OK for top dict but we still need EasyDict to transform nested dicts, etc.

So would be be great if:

leplatrem commented 10 months ago

Thanks for reporting :)

I like both options... Don't hesitate to open a pull-requests with unit tests