Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: Apurva Koti <[email protected]>
  • Loading branch information
apurva-koti committed May 14, 2024
1 parent f75d65d commit ccef2ae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
11 changes: 7 additions & 4 deletions mlflow/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,10 @@ class AttrDict(dict):
"""

def __getattr__(self, attr):
value = self[attr]
if isinstance(value, dict):
return AttrDict(value)
return value
try:
value = self[attr]
if isinstance(value, dict):
return AttrDict(value)
return value
except KeyError as e:
raise AttributeError(e)
16 changes: 16 additions & 0 deletions tests/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,19 @@ def test_random_name_generation():
names = [name_utils._generate_random_name() for i in range(1000)]
assert all(len(name) <= 20 for name in names)
assert all(name[-1].isnumeric() for name in names)


def test_attr_dict():
from mlflow.utils import AttrDict

d = {"a": 1, "b": {"c": 2}}
attr_d = AttrDict(d)
assert attr_d.a == 1
assert attr_d.b.c == 2


def test_attr_dict_hasattr():
from mlflow.utils import AttrDict
d = {"a": 1, "b": {"c": 2}}
attr_d = AttrDict(d)
assert not hasattr(attr_d, "nonexistent")

0 comments on commit ccef2ae

Please sign in to comment.