NOAA-EMC / wxflow

Tools for Weather Workflows
https://wxflow.readthedocs.io/en/latest
GNU Lesser General Public License v3.0
0 stars 7 forks source link

AttrDict returns empty AttrDict when key does not exist #14

Closed AndrewEichmann-NOAA closed 6 months ago

AndrewEichmann-NOAA commented 6 months ago

Expected behavior

When a non-existent key is used with an AttrDict, an exception should be thrown, assuming behavior is supposed to resemble a standard Python dictionary

Current behavior

When a key is not found, an empty AttrDict is returned

Machines affected

Hera

To Reproduce

#!/usr/bin/env python

from wxflow import AttrDict

local_dict = AttrDict(
    { "dasher": 'winken',
      "dancer": 'blinken',
      "comet":  'nod' }
     )

print(local_dict["dasher"])
print(type(local_dict["dasher"]))
print(local_dict["cupid"])
print(type(local_dict["cupid"]))
(gdasapp) -bash-4.2$ python attrdict.py 
winken
<class 'str'>
{}
<class 'wxflow.attrdict.AttrDict'>
(gdasapp) -bash-4.2$

Context

Trying to use paths in config structures with the type AttrDict and attempting to access non-existent entries as paths to be used as parameters to FileHandler results in confusing complaints from within FileHandler, which is expecting strings or path-like objects, not an empty AttrDict

Possible Implementation

Make it throw an exception like KeyError in the manner of dicts

aerorahul commented 6 months ago

@AndrewEichmann-NOAA Thank you for reporting the bug. The branch should resolve the bug where a missing key is throws a KeyError instead of an empty dict in a manner consistent with dict.