cdgriffith / Box

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

Confusing exceptions #218

Closed cwells closed 2 years ago

cwells commented 2 years ago

I would expect the following to raise a KeyError, not a BoxError:

In [17]: b = Box(box_dots=True)

In [18]: b['a.b']
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
lib64/python3.8/site-packages/box/box.py in __getitem__(self, item, _ignore_default)
    487         try:
--> 488             return super().__getitem__(item)
    489         except KeyError as err:

KeyError: 'a.b'

During handling of the above exception, another exception occurred:

BoxError                                  Traceback (most recent call last)
<ipython-input-18-e8e887ae1af6> in <module>
----> 1 b['a.b']

lib64/python3.8/site-packages/box/box.py in __getitem__(self, item, _ignore_default)
    493             if self._box_config["box_dots"] and isinstance(item, str) and ("." in item or "[" in item):
    494                 try:
--> 495                     first_item, children = _parse_box_dots(self, item)
    496                 except BoxError:
    497                     if self._box_config["default_box"] and not _ignore_default:

lib64/python3.8/site-packages/box/box.py in _parse_box_dots(bx, item, setting)
     91     if setting and "." in item:
     92         return item.split(".", 1)
---> 93     raise BoxError("Could not split box dots properly")
     94 
     95 

BoxError: Could not split box dots properly
cdgriffith commented 2 years ago

Agreed, that use case does not seem to be working properly. Thanks for bringing it up!

cdgriffith commented 2 years ago

Fixed in 6.0.1 https://github.com/cdgriffith/Box/releases/tag/6.0.1 Thanks again for pointing that out!