mahmoud / glom

☄️ Python's nested data operator (and CLI), for all your declarative restructuring needs. Got data? Glom it! ☄️
https://glom.readthedocs.io
Other
1.88k stars 61 forks source link

non-determinstic behavior on dict-like objects with attributes #243

Open ArthurKantor opened 2 years ago

ArthurKantor commented 2 years ago

reproduce with

import glom

class Attributes(dict):
    pass

target={'a':Attributes({'at1':1,'at2':2})}

spec=glom.Assign('a.at3',3)
res=glom.glom(target, spec)
print(res)

Sometimes the above prints {'a': {'at1': 1, 'at2': 2}}

and sometimes

{'a': {'at1': 1, 'at2': 2, 'at3': 3}}

some analysis

This seems to be caused by get_handler() sometimes returning setitem and sometimes returning setattr. (Maybe the correct behavior in this case is to disallow ambiguous paths. But in any case it should return the same thing consistently).

This in turn is caused by different orderings in the assign op type tree (I think the type tree should be constant):

OrderedDict([(<class 'object'>,
              OrderedDict([(<class 'glom.core._ObjStyleKeys'>, OrderedDict()),
                           (<class 'glom.core._AbstractIterable'>,
                            OrderedDict([(<class 'dict'>, OrderedDict()),
                                         (<class 'list'>, OrderedDict()),
                                         (<class 'tuple'>,
                                          OrderedDict())]))]))])

for the first result, and

OrderedDict([(<class 'object'>,
              OrderedDict([(<class 'glom.core._AbstractIterable'>,
                            OrderedDict([(<class 'dict'>, OrderedDict()),
                                         (<class 'list'>, OrderedDict()),
                                         (<class 'tuple'>, OrderedDict())])),
                           (<class 'glom.core._ObjStyleKeys'>,
                            OrderedDict())]))])

for the second.

ArthurKantor commented 2 years ago

this seems related to #233

kurtbrose commented 2 years ago

Thanks for the reproducing case and analysis. I agree it should be consistent and am surprised it isn't.