Skip to content

Commit

Permalink
Merge pull request #53 from marier-nico/fix/get-dict-value
Browse files Browse the repository at this point in the history
fix(util): fix get_value_at_path for None values
  • Loading branch information
marier-nico committed Jul 19, 2021
2 parents cb9a0dc + e36904f commit 83a522b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/event_processor/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ class InvocationError(EventProcessorError):

class DependencyError(EventProcessorError):
"""Exceptions for failures while resolving dependencies."""


class NoValueError(EventProcessorError):
"""Exception for when a value is not present in a given context."""
8 changes: 4 additions & 4 deletions src/event_processor/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Any, Union, Callable

from .dependencies import call_with_injection, Event
from .exceptions import FilterError
from .exceptions import FilterError, NoValueError
from .util import get_value_at_path


Expand Down Expand Up @@ -78,7 +78,7 @@ def __init__(self, path: Any):
def matches(self, event: dict) -> bool:
try:
get_value_at_path(event, self.path)
except KeyError:
except NoValueError:
return False

return True
Expand All @@ -102,7 +102,7 @@ def __init__(self, path: Any, value: Any):
def matches(self, event: dict) -> bool:
try:
return self.value == get_value_at_path(event, self.path)
except KeyError:
except NoValueError:
return False

def __hash__(self):
Expand Down Expand Up @@ -132,7 +132,7 @@ def matches(self, event: dict) -> bool:
try:
found_value = get_value_at_path(event, self.path)
float_value = float(found_value)
except (KeyError, ValueError):
except (NoValueError, ValueError):
return False

return self.comparator(float_value, self.target)
Expand Down
9 changes: 7 additions & 2 deletions src/event_processor/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@
from types import ModuleType
from typing import Any, List

from .exceptions import NoValueError


def get_value_at_path(source: dict, path: str) -> Any:
"""Get the value in a source dict at the given path.
:param source: The dict from which to extract the value
:param path: The path to look into
:return: The value found at the path
:raises: KeyError when no value exists at the path
:raises: NoValueError when no value exists at the path
"""
current_location = source
for part in path.split("."):
current_location = current_location[part]
try:
current_location = current_location[part]
except (KeyError, TypeError):
raise NoValueError(f"No value at the path '{path}'")

return current_location

Expand Down
10 changes: 9 additions & 1 deletion src/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest

from src.event_processor.exceptions import NoValueError
from src.event_processor.util import get_value_at_path, py37_get_args, py37_get_origin, load_all_modules_in_package


Expand All @@ -18,10 +19,17 @@ def test_get_value_at_path_gets_the_value_when_it_exists():
def test_get_value_at_path_raises_for_an_invalid_path():
path = "a.b.c"

with pytest.raises(KeyError):
with pytest.raises(NoValueError):
get_value_at_path({"a": {"b": {"not-c": None}}}, path)


def test_get_value_at_path_raises_for_none_value():
path = "a.b.c"

with pytest.raises(NoValueError):
get_value_at_path({"a": None}, path)


def test_py37_get_args_returns_generic_for_generic_type():
assert py37_get_args(typing.Generic) is typing.Generic

Expand Down

0 comments on commit 83a522b

Please sign in to comment.