Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: do not auto convert enums to values for fetching attrs #138

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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