cdgriffith / Box

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

setdefault with list value is broken in 5.4.0 #205

Closed ipspace closed 3 years ago

ipspace commented 3 years ago

Instead of setting an attribute of a box to a list value (as before), setdefault sets the attribute value to an empty dict.

>>> from box import Box
>>> t = Box({'a':1},default_box=True,box_dots=True,default_box_none_transform=False)
>>> t
<Box: {'a': 1}>
>>> t.setdefault('b',[1,2])
<Box: {}>
>>> t
<Box: {'a': 1, 'b': {}}>

Environment:

$ python --version
Python 3.8.10
$ uname -a
Linux brick 5.4.0-77-generic #86-Ubuntu SMP Thu Jun 17 02:35:03 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
ipspace commented 3 years ago

The error seems to be here:

    try:
            return self[item]

... which auto-creates the item as an empty Box dictionary when default_box is set.

cdgriffith commented 3 years ago

Added a hopeful fix in develop. Testing would be appreciated as I am not keen on pushing by myself ATM as I am recovering from surgery this weekend so a littly loopy.

ipspace commented 3 years ago

Used the new code in my project (https://github.com/ipspace/netsim-tools). It uses Box to do extensive data transformation, and the tests cover as many edge cases as possible. Tests passed using Box develop branch and failed using Box master branch, proving Box code from develop branch works the same way as version 5.2.0 (at least the way I use it).

Wrote several setdefault tests and submitted a pull request (#207).

Hope you'll recover in no time! Don't push yourself, code will wait ;)

All the best, Ivan

cdgriffith commented 3 years ago

Fixed in 5.4.1 https://github.com/cdgriffith/Box/releases/tag/5.4.1 thank you so much for the help!

I will have to give a look through that tool as well, may be helpful in some of my work!