mewwts / addict

The Python Dict that's better than heroin.
MIT License
2.46k stars 132 forks source link

Keys starting with __ can't be referenced with dotted notation inside an object #149

Open ramonfm opened 2 years ago

ramonfm commented 2 years ago

See below; keys that start with can't be referenced inside an object with dotted notation. If I define missing__() I get "KeyError: '_Stuff__metadata'".

Thanks for this awesome piece of software BTW -- makes my life much better.

import addict
from pprint import pformat

data = {
    'foo': 7,
    'bar': {
        'metadata': {'text': 'this works', 'value': True},
        '__metadata': {'text': 'this works', 'value': False}
    }
}
dotted = addict.Dict(data)

class Stuff(object):
    def __init__(self):
        pass

    def run(self):
        assert pformat(data['bar']) == pformat(dotted.bar), 'text representation does not work'
        assert dotted.bar.metadata.text == dotted.bar['__metadata'].text, 'mixed reference broken inside an object'
        assert dotted.bar.metadata.text == dotted.bar.__metadata.text, 'dotted reference broken inside an object'

assert dotted.bar.metadata.text == dotted.bar.__metadata.text, 'broken outside an object'
Stuff().run()