Skip to content

Commit

Permalink
Merge pull request #38 from marier-nico/fix/scalar-value-dependency-w…
Browse files Browse the repository at this point in the history
…ithout-pydantic

Fix scalar value resolution without pydantic
  • Loading branch information
marier-nico committed May 19, 2021
2 parents f9b986f + a526f98 commit 9a1d7da
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
1 change: 1 addition & 0 deletions docs/content/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- v2.4.1: Fix scalar dependency resolution without pydantic (only raise on actual missing values and not none values)
- v2.4.0: Support scalar value dependencies in processor parameters
- v2.3.1: Raise the correct exception when processor parameters are invalid due to optional args
- v2.3.0: Support dynamic filters
Expand Down
6 changes: 3 additions & 3 deletions src/event_processor/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,11 @@ def resolve_scalar_value_dependencies_without_pydantic(
"""
resolved = {}
for param in scalar_dependencies:
if event.get(param.name) is None:
try:
resolved[param.name] = event[param.name]
except KeyError:
raise DependencyError(f"No value found in event for param '{param.name}'")

resolved[param.name] = event[param.name]

return resolved


Expand Down
35 changes: 22 additions & 13 deletions src/tests/test_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def fn(_a: BaseModel):
def test_resolve_scalar_value_dependencies_resolves_without_pydantic_when_pydantic_is_not_installed(resolve_mock):
params = [inspect.Parameter("x", kind=inspect.Parameter.POSITIONAL_ONLY)]

resolve_scalar_value_dependencies(params, {})
resolve_scalar_value_dependencies(params, Event({}))

resolve_mock.assert_called_once_with(params, {})

Expand All @@ -371,7 +371,7 @@ def test_resolve_scalar_value_dependencies_resolves_without_pydantic_when_pydant
def test_resolve_scalar_value_dependencies_resolves_with_pydantic_when_pydantic_is_installed(resolve_mock):
params = [inspect.Parameter("x", kind=inspect.Parameter.POSITIONAL_ONLY)]

resolve_scalar_value_dependencies(params, {})
resolve_scalar_value_dependencies(params, Event({}))

resolve_mock.assert_called_once_with(params, {})

Expand All @@ -386,7 +386,7 @@ def fn():


def test_get_scalar_value_dependencies_does_not_return_variable_arguments():
def fn(x, *args, **kwargs):
def fn(_x, *_args, **_kwargs):
pass

dependencies = get_scalar_value_dependencies(fn)
Expand All @@ -407,24 +407,33 @@ def test_resolve_scalar_value_dependencies_without_pydantic_fetches_values_from_
event = {"x": 0}
scalar_dependencies = [inspect.Parameter("x", kind=inspect.Parameter.POSITIONAL_ONLY)]

result = resolve_scalar_value_dependencies_without_pydantic(scalar_dependencies, event)
result = resolve_scalar_value_dependencies_without_pydantic(scalar_dependencies, Event(event))

assert result == {"x": 0}


def test_resolve_scalar_value_dependencies_without_pydantic_does_not_fail_on_none_value():
event = {"x": None}
scalar_dependencies = [inspect.Parameter("x", kind=inspect.Parameter.POSITIONAL_ONLY)]

result = resolve_scalar_value_dependencies_without_pydantic(scalar_dependencies, Event(event))

assert result == {"x": None}


def test_resolve_scalar_value_dependencies_without_pydantic_raises_on_missing_arg_value():
event = {"not-x": 0}
scalar_dependencies = [inspect.Parameter("x", annotation=int, kind=inspect.Parameter.POSITIONAL_ONLY)]

with pytest.raises(DependencyError):
resolve_scalar_value_dependencies_without_pydantic(scalar_dependencies, event)
resolve_scalar_value_dependencies_without_pydantic(scalar_dependencies, Event(event))


def test_resolve_scalar_value_dependencies_with_pydantic_fetches_values_from_event():
event = {"x": 0}
scalar_dependencies = [inspect.Parameter("x", annotation=int, kind=inspect.Parameter.POSITIONAL_ONLY)]

result = resolve_scalar_value_dependencies_with_pydantic(scalar_dependencies, event)
result = resolve_scalar_value_dependencies_with_pydantic(scalar_dependencies, Event(event))

assert result == {"x": 0}

Expand All @@ -434,31 +443,31 @@ def test_resolve_scalar_value_dependencies_with_pydantic_raises_on_missing_arg_v
scalar_dependencies = [inspect.Parameter("x", annotation=int, kind=inspect.Parameter.POSITIONAL_ONLY)]

with pytest.raises(ValidationError):
resolve_scalar_value_dependencies_with_pydantic(scalar_dependencies, event)
resolve_scalar_value_dependencies_with_pydantic(scalar_dependencies, Event(event))


def test_resolve_scalar_value_dependencies_with_pydantic_raises_on_validation_errors():
event = {"x": "not-an-int"}
scalar_dependencies = [inspect.Parameter("x", annotation=int, kind=inspect.Parameter.POSITIONAL_ONLY)]

with pytest.raises(ValidationError):
resolve_scalar_value_dependencies_with_pydantic(scalar_dependencies, event)
resolve_scalar_value_dependencies_with_pydantic(scalar_dependencies, Event(event))


def test_resolve_scalar_value_dependencies_with_pydantic_raises_on_validation_errors_for_pydantic_field_types():
event = {"x": "not-an-int"}
scalar_dependencies = [inspect.Parameter("x", annotation=Color, kind=inspect.Parameter.POSITIONAL_ONLY)]

with pytest.raises(ValidationError):
resolve_scalar_value_dependencies_with_pydantic(scalar_dependencies, event)
resolve_scalar_value_dependencies_with_pydantic(scalar_dependencies, Event(event))


@pytest.mark.parametrize("event_val", ["some-string", {"a": "dict"}])
def test_resolve_scalar_value_dependencies_with_pydantic_accepts_any_without_annotations(event_val):
event = {"x": event_val}
scalar_dependencies = [inspect.Parameter("x", kind=inspect.Parameter.POSITIONAL_ONLY)]

result = resolve_scalar_value_dependencies_with_pydantic(scalar_dependencies, event)
result = resolve_scalar_value_dependencies_with_pydantic(scalar_dependencies, Event(event))

assert result["x"] == event_val

Expand All @@ -467,20 +476,20 @@ def test_resolve_scalar_value_dependencies_with_pydantic_makes_values_required_w
scalar_dependencies = [inspect.Parameter("x", kind=inspect.Parameter.POSITIONAL_ONLY)]

with pytest.raises(ValidationError):
resolve_scalar_value_dependencies_with_pydantic(scalar_dependencies, {})
resolve_scalar_value_dependencies_with_pydantic(scalar_dependencies, Event({}))


def test_resolve_scalar_value_dependencies_with_pydantic_makes_values_optional_when_a_default_is_provided():
scalar_dependencies = [inspect.Parameter("x", default="default", kind=inspect.Parameter.POSITIONAL_ONLY)]

result = resolve_scalar_value_dependencies_with_pydantic(scalar_dependencies, {})
result = resolve_scalar_value_dependencies_with_pydantic(scalar_dependencies, Event({}))

assert result["x"] == "default"


def test_resolve_scalar_value_dependencies_with_pydantic_passes_none_to_unfilled_optionals():
scalar_dependencies = [inspect.Parameter("x", annotation=Optional[str], kind=inspect.Parameter.POSITIONAL_ONLY)]

result = resolve_scalar_value_dependencies_with_pydantic(scalar_dependencies, {})
result = resolve_scalar_value_dependencies_with_pydantic(scalar_dependencies, Event({}))

assert result["x"] is None

0 comments on commit 9a1d7da

Please sign in to comment.