Skip to content

Commit

Permalink
context: tweak erros and add tests for context lookup errors
Browse files Browse the repository at this point in the history
This commit tweaks the error message for the conext generated
errors and adds tests.
  • Loading branch information
mvo5 committed Oct 2, 2024
1 parent e0b3775 commit 6191cff
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/otk/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,17 @@ def variable(self, name: str) -> Any:
value = value[part]
elif isinstance(value, list):
if not part.isnumeric():
raise TransformVariableIndexTypeError("part is not numeric")
raise TransformVariableIndexTypeError(f"part is not numeric but {type(part)}")

try:
value = value[int(part)]
except IndexError as exc:
raise TransformVariableIndexRangeError(f"{part} is out of range") from exc
raise TransformVariableIndexRangeError(f"{part} is out of range for {value}") from exc
else:
prefix = ".".join(parts[:i])
raise TransformVariableTypeError(
f"tried to look up '{prefix}.{part}', but prefix '{prefix}' value {value!r} is not a dictionary")
f"tried to look up '{prefix}.{part}', but the value of "
f"prefix '{prefix}' is not a dictionary but {type(value)}")

return value

Expand Down
15 changes: 12 additions & 3 deletions test/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,27 @@ def test_context_nonexistent():

def test_context_unhappy():
ctx = CommonContext()

with pytest.raises(TransformVariableLookupError) as exc:
ctx.variable("foo")
assert "could not resolve 'foo' as 'foo' is not defined" in str(exc.value)

ctx.define("foo", "foo")

with pytest.raises(TransformVariableTypeError):
with pytest.raises(TransformVariableTypeError) as exc:
ctx.variable("foo.bar")
assert "tried to look up 'foo.bar', but the value of prefix 'foo' is not a dictionary but <class 'str'>" in str(
exc.value)

ctx.define("bar", ["bar"])

with pytest.raises(TransformVariableIndexTypeError):
with pytest.raises(TransformVariableIndexTypeError) as exc:
ctx.variable("bar.bar")
assert "part is not numeric but <class 'str'>" in str(exc.value)

with pytest.raises(TransformVariableIndexRangeError):
with pytest.raises(TransformVariableIndexRangeError) as exc:
ctx.variable("bar.3")
assert "3 is out of range for ['bar']" in str(exc.value)


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion test/test_substitute_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_sub_var_missing_var_in_context():
substitute_vars(context, state, "${a.b}")
# no subtree
context.define("a", "foo")
expected_err = r"tried to look up 'a.b', but prefix 'a' value 'foo' is not a dictionary"
expected_err = r"tried to look up 'a.b', but the value of prefix 'a' is not a dictionary but <class 'str'>"
with pytest.raises(TransformVariableTypeError, match=expected_err):
substitute_vars(context, state, "${a.b}")

Expand Down

0 comments on commit 6191cff

Please sign in to comment.