cdgriffith / Box

Python dictionaries with advanced dot notation access
https://github.com/cdgriffith/Box/wiki
MIT License
2.61k stars 106 forks source link

box.box.Box.setdefault return wrong value if value is dictionary #157

Closed NoamGraetz2 closed 4 years ago

NoamGraetz2 commented 4 years ago

in short, it should return self[item] instead of default

in details - if the value is dictionary, it is transformed to box internally in the "self[item] = default" call. in this case, returning 'default' will return a detached empty dictionary that is usless

fixed code should look like: def setdefault(self, item, default=None): if item in self: return self[item]

    if isinstance(default, dict):
        default = self._box_config["box_class"](default, **self.__box_config())
    if isinstance(default, list):
        default = box.BoxList(default, **self.__box_config())
    self[item] = default
    return self[item]