Skip to content

Commit

Permalink
feat!: do not auto convert enums to values for fetching attrs
Browse files Browse the repository at this point in the history
The attrgetters no longer auto convert enums
to their values

These objects are str enums so convert should not be needed
  • Loading branch information
bdraco committed Jul 2, 2024
1 parent e4d53c1 commit 16cc25c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 27 deletions.
2 changes: 0 additions & 2 deletions src/uiprotect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from .utils import (
get_nested_attr,
get_nested_attr_as_bool,
get_top_level_attr,
get_top_level_attr_as_bool,
make_enabled_getter,
make_required_getter,
Expand All @@ -21,7 +20,6 @@
"ProtectApiClient",
"get_nested_attr",
"get_nested_attr_as_bool",
"get_top_level_attr",
"get_top_level_attr_as_bool",
"make_value_getter",
"make_enabled_getter",
Expand Down
15 changes: 4 additions & 11 deletions src/uiprotect/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ def get_nested_attr(attrs: tuple[str, ...], obj: Any) -> Any:
for key in attrs:
if (value := getattr(value, key, _SENTINEL)) is _SENTINEL:
return None
return value.value if isinstance(value, Enum) else value
return value


def get_nested_attr_as_bool(attrs: tuple[str, ...], obj: Any) -> bool:
Expand All @@ -643,25 +643,18 @@ def get_nested_attr_as_bool(attrs: tuple[str, ...], obj: Any) -> bool:
for key in attrs:
if (value := getattr(value, key, _SENTINEL)) is _SENTINEL:
return False
return bool(value.value if isinstance(value, Enum) else value)


def get_top_level_attr(attr: str, obj: Any) -> Any:
"""Fetch a top level attribute."""
value = getattr(obj, attr)
return value.value if isinstance(value, Enum) else value
return bool(value)


def get_top_level_attr_as_bool(attr: str, obj: Any) -> Any:
"""Fetch a top level attribute as a bool."""
value = getattr(obj, attr)
return bool(value.value if isinstance(value, Enum) else value)
return bool(getattr(obj, attr))


def make_value_getter(ufp_value: str) -> Callable[[T], Any]:
"""Return a function to get a value from a Protect device."""
if "." not in ufp_value:
return partial(get_top_level_attr, ufp_value)
return attrgetter(ufp_value)
return partial(get_nested_attr, tuple(ufp_value.split(".")))


Expand Down
18 changes: 4 additions & 14 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
dict_diff,
get_nested_attr,
get_nested_attr_as_bool,
get_top_level_attr,
get_top_level_attr_as_bool,
make_enabled_getter,
make_required_getter,
Expand Down Expand Up @@ -219,7 +218,7 @@ def test_get_nested_attr():
data = Mock(a=Mock(b=Mock(c=1)), d=3, f=_MockEnum.C)
assert get_nested_attr(("a", "b", "c"), data) == 1
assert get_nested_attr(("d",), data) == 3
assert get_nested_attr(("f",), data) == _MockEnum.C.value
assert get_nested_attr(("f",), data) == _MockEnum.C


@pytest.mark.asyncio
Expand All @@ -230,18 +229,9 @@ def test_get_nested_attr_as_bool():
assert get_nested_attr_as_bool(("f",), data) is True


@pytest.mark.asyncio
def test_get_top_level_attr():
data = Mock(a=1, b=2, c=3, d=_MockEnum.C)
assert get_top_level_attr("a", data) == 1
assert get_top_level_attr("b", data) == 2
assert get_top_level_attr("c", data) == 3
assert get_top_level_attr("d", data) == _MockEnum.C.value


@pytest.mark.asyncio
def test_get_top_level_attr_as_bool():
data = Mock(a=True, b=False, c=_MockEnum.C, d=None)
data = Mock(a=True, b=False, c=True, d=None)
assert get_top_level_attr_as_bool("a", data) is True
assert get_top_level_attr_as_bool("b", data) is False
assert get_top_level_attr_as_bool("c", data) is True
Expand All @@ -250,11 +240,11 @@ def test_get_top_level_attr_as_bool():

@pytest.mark.asyncio
def test_make_value_getter():
data = Mock(a=1, b=2, c=3, d=_MockEnum.C)
data = Mock(a=1, b=2, c=3, d=4)
assert make_value_getter("a")(data) == 1
assert make_value_getter("b")(data) == 2
assert make_value_getter("c")(data) == 3
assert make_value_getter("d")(data) == _MockEnum.C.value
assert make_value_getter("d")(data) == 4


@pytest.mark.asyncio
Expand Down

0 comments on commit 16cc25c

Please sign in to comment.